Python

Statements

StatementDescription
importImport modules to be used in the code
=Assignment
delDeletes a variable
ifConditional execution
elifConditional execution, "else if"
forIterate over iterable object, e.g. a list
whileLoop for as long as its condition is true
breakExits a loop
continueSkips to the next cycle in a loop
defDefines a function or method
returnReturn values from function or method
yieldUsed to implement coroutines
classFor creating classes in object-oriented programming
withUse of context manager for managing resources
tryCatches exceptions raised by the attached code
raiseRaise an exception
assertRaise error if condition is not met
passNeeded to create empty code blocks

Expressions

ExpressionDescription
+, -, *, **, /, //Mathematical operations
==, <, <=, >, >=Comparisons
+String and list concatenation
*String and list duplication
@Infix operator for use in matrix multiplication
:="Walrus operator" assigns values as part of a larger expression
and, or, notBoolean operators
[ i for i in iterable ]List comprehension (generator expression)
{ k: i for k, i in zip(iterable, iterable) }Dictionary comprehension (generator expression)
lambda x: xAnonymous function
x if c else yConditional expression
a, b = 1, 2Sequence unpacking
"string", 'string'Strings
"""string"""Multiline string
f"1 + 2 = {1 + 2}"F-string
r"string\n"Raw string (no escapes)
my_list[index]Array indexing
my_list[start:stop:step]Array slicing

Types

TypeMutabilityDescriptionExample
boolimmutableBooleanTrue, False
intimmutableInteger15
rangeimmutableSequence of integersrange(1, 10, 2)
floatimmutableDouble-precision floating-point number2.66666
compleximmutableComplex number1.5 + 3.2j
strimmutableCharacter string (Unicode)"Text", 'Text'
bytesimmutableSequence of bytesb"Some ASCII"
bytearraymutableSequence of bytes
listmutableList[1, 2, 3]
tupleimmutableImmutable list(1, 2, 3)
dictmutableDictionary{"a": 1, "b": 2}
setmutableUnordered set{"a", "b", "c"}
frozensetimmutableUnordered set
types.NoneTypeimmutableAbsence of value, nullNone
types.NotImplementedTypeimmutableIndicates unsupported operand typesNotImplemented
types.EllipsisTypeimmutableEllipsis placeholder for NumPy arrays..., Ellipsis

Useful Built-in Functions

FunctionDescription
print()Prints to the console
zip()Iterate over multiple iterables
itertools.product()Create combinations from iterables