Skip to content

Python

StatementDescription
:star:importImport modules to be used in the code
:star:=Assignment
delDeletes a variable
:star:ifConditional execution
:star:elifConditional execution, “else if”
:star: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
:star:defDefines a function or method
:star: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
ExpressionDescription
:star:+, -, *, **, /, //Mathematical operations
:star:==, <, <=, >, >=Comparisons
:star:+String and list concatenation
:star:*String and list duplication
@Infix operator for use in matrix multiplication
:=”Walrus operator” assigns values as part of a larger expression
:star:and, or, notBoolean operators
:star:[ i for i in iterable ]List comprehension (generator expression)
:star:{ k: i for k, i in zip(iterable, iterable) }Dictionary comprehension (generator expression)
:star:lambda x: xAnonymous function
:star:x if c else yConditional expression
:star:a, b = 1, 2Sequence unpacking
:star:"string", 'string'Strings
:star:"""string"""Multiline string
:star:f"1 + 2 = {1 + 2}"F-string
r"string\n"Raw string (no escapes)
:star:my_list[index]Array indexing
:star:my_list[start:stop:step]Array slicing
TypeMutabilityDescriptionExample
:star:boolimmutableBooleanTrue, False
:star:intimmutableInteger15
:star:rangeimmutableSequence of integersrange(1, 10, 2)
:star:floatimmutableDouble-precision floating-point number2.66666
compleximmutableComplex number1.5 + 3.2j
:star:strimmutableCharacter string (Unicode)"Text", 'Text'
bytesimmutableSequence of bytesb"Some ASCII"
bytearraymutableSequence of bytes
:star:listmutableList[1, 2, 3]
:star:tupleimmutableImmutable list(1, 2, 3)
:star:dictmutableDictionary{"a": 1, "b": 2}
:star:setmutableUnordered set{"a", "b", "c"}
frozensetimmutableUnordered set
:star:types.NoneTypeimmutableAbsence of value, nullNone
types.NotImplementedTypeimmutableIndicates unsupported operand typesNotImplemented
types.EllipsisTypeimmutableEllipsis placeholder for NumPy arrays..., Ellipsis
FunctionDescription
print()Prints to the console
zip()Iterate over multiple iterables
itertools.product()Create combinations from iterables