Python
Statements
Statement | Description | |
---|---|---|
⭐ | import | Import modules to be used in the code |
⭐ | = | Assignment |
del | Deletes a variable | |
⭐ | if | Conditional execution |
⭐ | elif | Conditional execution, "else if" |
⭐ | 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 | |
⭐ | def | Defines a function or method |
⭐ | 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
Expression | Description | |
---|---|---|
⭐ | + , - , * , ** , / , // | 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 , not | Boolean 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: x | Anonymous function |
⭐ | x if c else y | Conditional expression |
⭐ | a, b = 1, 2 | Sequence 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
Type | Mutability | Description | Example | |
---|---|---|---|---|
⭐ | bool | immutable | Boolean | True , False |
⭐ | int | immutable | Integer | 15 |
⭐ | range | immutable | Sequence of integers | range(1, 10, 2) |
⭐ | float | immutable | Double-precision floating-point number | 2.66666 |
complex | immutable | Complex number | 1.5 + 3.2j | |
⭐ | str | immutable | Character string (Unicode) | "Text" , 'Text' |
bytes | immutable | Sequence of bytes | b"Some ASCII" | |
bytearray | mutable | Sequence of bytes | ||
⭐ | list | mutable | List | [1, 2, 3] |
⭐ | tuple | immutable | Immutable list | (1, 2, 3) |
⭐ | dict | mutable | Dictionary | {"a": 1, "b": 2} |
⭐ | set | mutable | Unordered set | {"a", "b", "c"} |
frozenset | immutable | Unordered set | ||
⭐ | 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
Function | Description |
---|---|
print() | Prints to the console |
zip() | Iterate over multiple iterables |
itertools.product() | Create combinations from iterables |
Table of Contents