Home › Python Tutorial › All commands
All commands
Every command of this course: name, detailed description, syntax and example code.
Every command used in this course, with its name, a detailed description, the syntax and an example.
print
Definition: Shows values on the screen. Several values separated by commas are printed with spaces between them.
Syntax:
print(value, ...)
Example:
print("Hello", 5)input
Definition: Pauses the program and returns what the user typed, always as text (a string).
Syntax:
input(prompt)
Example:
name = input("Name: ")def
Definition: Defines a function. Indentation shows which lines belong to it; return sends a value back.
Syntax:
def name(parameters):
return valueExample:
def add(a, b):
return a + bif / elif / else
Definition: Chooses which indented block to run. Each condition ends with a colon.
Syntax:
if condition:
...
elif other:
...
else:
...Example:
if x > 3:
print("big")for
Definition: Repeats a block for every item of a sequence such as a list or range().
Syntax:
for item in sequence:
...Example:
for i in range(3):
print(i)while
Definition: Repeats an indented block while its condition is true.
Syntax:
while condition:
...Example:
n = 0
while n < 3:
n += 1len
Definition: Returns the number of items in a list, string, tuple or dictionary.
Syntax:
len(object)
Example:
len("hello") # 5range
Definition: Produces a sequence of numbers, handy for loops: range(stop) or range(start, stop, step).
Syntax:
range(start, stop, step)
Example:
list(range(1, 4)) # [1, 2, 3]
append
Definition: Adds one item to the end of a list.
Syntax:
list.append(item)
Example:
a = [1, 2] a.append(3)
import
Definition: Loads a module so you can use its functions, such as math or random.
Syntax:
import module
Example:
import math print(math.sqrt(16))
#
Definition: Comments start with # .
Syntax:
#
Example:
# Indentation defines the block
type()
Definition: print shows values separated by spaces; type() tells you the type.
Syntax:
type()
Example:
print(type(x))
int
Definition: Whole numbers are int , decimals are float .
Syntax:
int
Example:
print(int(c) + a)
upper
Definition: Methods like upper and replace return a new string.
Syntax:
upper
Example:
print(s.upper())
replace
Definition: Methods like upper and replace return a new string.
Syntax:
replace
Example:
print(s.replace("World", "Python"))and
Definition: Combine them with and , or and not .
Syntax:
and
Example:
print(x > 5 and x < 20)
not
Definition: Combine them with and , or and not .
Syntax:
not
Example:
print(not x == 10)
sort
Definition: append adds an item, sort orders them, and a negative index counts from the end.
Syntax:
sort
Example:
fruits.sort()
get
Definition: Use [key] to read or set, get for a safe read with a default, and items() to loop over keys and values together.
Syntax:
get
Example:
print(p.get("zip", "n/a"))items()
Definition: Use [key] to read or set, get for a safe read with a default, and items() to loop over keys and values together.
Syntax:
items()
Example:
for k, v in p.items():
if
Definition: Use if , elif and else .
Syntax:
if
Example:
if score >= 90:
print("pass" if score >= 60 else "fail")else
Definition: Use if , elif and else .
Syntax:
else
Example:
else:
print("pass" if score >= 60 else "fail")match
Definition: match compares a value against case patterns.
Syntax:
match
Example:
match day:
case
Definition: match compares a value against case patterns.
Syntax:
case
Example:
case 1: case 2: case 3:
lambda
Definition: A lambda is a small one-line function.
Syntax:
lambda
Example:
square = lambda n: n * n
math
Definition: The standard library includes math , datetime , random , json and many more.
Syntax:
math
Example:
import math print(math.sqrt(81), math.pi)
datetime
Definition: The standard library includes math , datetime , random , json and many more.
Syntax:
datetime
Example:
import datetime d = datetime.date(2026, 12, 25)
strftime
Definition: strftime formats a date as text.
Syntax:
strftime
Example:
print(d.strftime("%A, %d %B %Y"))json.dumps
Definition: json.dumps turns a dictionary into JSON text and json.loads turns it back.
Syntax:
json.dumps
Example:
text = json.dumps(data)
json.loads
Definition: json.dumps turns a dictionary into JSON text and json.loads turns it back.
Syntax:
json.loads
Example:
print(json.loads(text)["langs"][0])
re
Definition: The re module finds patterns in text; [0-9]+ matches one or more digits.
Syntax:
re
Example:
import json, re
print(re.findall("[0-9]+", "a1 b22 c333"))try
Definition: Put risky code in try and handle errors in except .
Syntax:
try
Example:
try:
except
Definition: Put risky code in try and handle errors in except .
Syntax:
except
Example:
except ValueError:
finally
Definition: else runs if nothing failed and finally always runs.
Syntax:
finally
Example:
finally:
None
Definition: None means no value; test it with is None .
Syntax:
None
Example:
x = None print(x is None)
__init__
Definition: __init__ runs when an object is created, and self is the object itself.
Syntax:
__init__
Example:
def __init__(self, name):
self
Definition: __init__ runs when an object is created, and self is the object itself.
Syntax:
self
Example:
def __init__(self, name): self.name = name def speak(self):
speak()
Definition: Polymorphism means the same call ( speak() ) behaves differently for different classes.
Syntax:
speak()
Example:
def speak(self): print(a.speak())
__balance
Definition: A name starting with two underscores ( __balance ) is private to the class.
Syntax:
__balance
Example:
self.__balance = balance
self.__balance += n
return f"Balance: {self.__balance}"__str__
Definition: Magic methods such as __str__ control how an object prints.
Syntax:
__str__
Example:
def __str__(self):
open(name, mode)
Definition: open(name, mode) uses modes "r" read, "w" write (replaces the file) and "a" append.
Syntax:
open(name, mode)
Example:
with open("notes.txt", "w") as f:
with open("notes.txt") as f:with
Definition: The with statement closes the file automatically.
Syntax:
with
Example:
with open("notes.txt", "w") as f:
with open("notes.txt") as f:os.remove
Definition: os.remove deletes a file.
Syntax:
os.remove
Example:
os.remove("notes.txt")statistics
Definition: The statistics module gives the mean (average), median (middle value), mode (most common) and standard deviation (spread).
Syntax:
statistics
Example:
import statistics as st
pop
Definition: A stack is last-in-first-out (a list with append and pop ).
Syntax:
pop
Example:
print(stack.pop())
sqlite3
Definition: The built-in sqlite3 module works the same way and needs no server, so it is a good way to practise.
Syntax:
sqlite3
Example:
import sqlite3
con = sqlite3.connect(":memory:")Example code
print("Hello, Python!")