Python
Statements
Section titled “Statements”Statement | Description | |
---|---|---|
:star: | import | Import modules to be used in the code |
:star: | = | Assignment |
del | Deletes a variable | |
:star: | if | Conditional execution |
:star: | elif | Conditional execution, “else if” |
:star: | for | Iterate over iterable object, e.g. a list |
while | Loop for as long as its condition is true | |
break | Exits a loop | |
continue | Skips to the next cycle in a loop | |
:star: | def | Defines a function or method |
:star: | return | Return values from function or method |
yield | Used to implement coroutines | |
class | For creating classes in object-oriented programming | |
with | Use of context manager for managing resources | |
try | Catches exceptions raised by the attached code | |
raise | Raise an exception | |
assert | Raise error if condition is not met | |
pass | Needed to create empty code blocks |
Expressions
Section titled “Expressions”Expression | Description | |
---|---|---|
: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 , not | Boolean 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: x | Anonymous function |
:star: | x if c else y | Conditional expression |
:star: | a, b = 1, 2 | Sequence 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 |
Type | Mutability | Description | Example | |
---|---|---|---|---|
:star: | bool | immutable | Boolean | True , False |
:star: | int | immutable | Integer | 15 |
:star: | range | immutable | Sequence of integers | range(1, 10, 2) |
:star: | float | immutable | Double-precision floating-point number | 2.66666 |
complex | immutable | Complex number | 1.5 + 3.2j | |
:star: | str | immutable | Character string (Unicode) | "Text" , 'Text' |
bytes | immutable | Sequence of bytes | b"Some ASCII" | |
bytearray | mutable | Sequence of bytes | ||
:star: | list | mutable | List | [1, 2, 3] |
:star: | tuple | immutable | Immutable list | (1, 2, 3) |
:star: | dict | mutable | Dictionary | {"a": 1, "b": 2} |
:star: | set | mutable | Unordered set | {"a", "b", "c"} |
frozenset | immutable | Unordered set | ||
:star: | types.NoneType | immutable | Absence of value, null | None |
types.NotImplementedType | immutable | Indicates unsupported operand types | NotImplemented | |
types.EllipsisType | immutable | Ellipsis placeholder for NumPy arrays | ... , Ellipsis |
Useful Built-in Functions
Section titled “Useful Built-in Functions”Function | Description |
---|---|
print() | Prints to the console |
zip() | Iterate over multiple iterables |
itertools.product() | Create combinations from iterables |