Operators
Assignment |
|
Arithmetic |
|
Comparison |
|
Logical |
|
String operations (string s)
|
Count occurences |
|
Index of first occurence |
|
Concatenate sequence |
|
Split into list |
List operations (list l, element e)
|
Add e |
|
Remove e |
|
Remove and return e |
|
Count occurences |
|
Reverse l |
|
Sort l |
Dictionary operations (dict d, key k)
|
Clear d |
|
Return d[k] |
|
Return keys in d |
|
Return values in d |
|
Return key-value pairs in d |
File operations (file f)
|
Open file at path as f |
|
Read f |
|
Read line from f |
|
Return list of lines in f |
|
Write s to f |
|
Close f |
When using:
with open(path) as f:
the file gets opened as f and closes after leaving the "with" statement
|
|
Base functions
int(), float(), str(), bool() ...
|
Type casting |
|
Return length of data |
min(values), max(values)
|
Minimum / Maximum |
|
x to the power y [mod z] |
range(start, stop, [step])
|
Ordered list |
|
Console input/ output |
filter(function, iterable)
|
Filter iterable |
|
Map function onto iterable |
|
Unique object ID |
|
Round n [x decimal places] |
create your own functions with:
def functioname:
Control flow
if(cond): <code> else: <code>
|
If-else statement |
if(cond): <code> elif(cond): <code> else: <code>
|
If-elseif-else statement |
for i in range([start], stop, [step]): <code>
|
For loop over range |
|
For loop over iterable |
|
While loop |
|
Exit loop |
|
Skip to next iteration |
|
|
Object-oriented
|
Class definition |
|
Object creation |
|
Filed access |
|
Method access |
List comprehensions
List comprehensions can be used to generate lists with the use of functions in just one line |
S = [x**2 for x in range(10)]
|
M = [x for x in S if x % 2 == 0]
|
noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]
|
primes = [x for x in range(2, 50) if x not in noprimes]
|
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
|
Module Import
|
Imports a module |
|
Imports a module as x |
from module import submodule
|
Imports specific submodule |
|