Operators
Assignment |
=
|
Arithmetic |
+ , - , * , / , %
|
Comparison |
> , >= , < , <= , == , !=
|
Logical |
not, and, or
|
String operations (string s)
s.count(substring)
|
Count occurences |
s.find(substring)
|
Index of first occurence |
s.join(sequence)
|
Concatenate sequence |
s.split([delimiter])
|
Split into list |
List operations (list l, element e)
l.append(e)
|
Add e |
l.remove(e)
|
Remove e |
l.pop(e)
|
Remove and return e |
l.count(e)
|
Count occurences |
l.reverse()
|
Reverse l |
l.sort()
|
Sort l |
Dictionary operations (dict d, key k)
d.clear()
|
Clear d |
d.get(k)
|
Return d[k] |
d.keys()
|
Return keys in d |
d.values()
|
Return values in d |
d.items()
|
Return key-value pairs in d |
|
|
File operations (file f)
f = open(path)
|
Open f |
f.read()
|
Read f |
f.readline()
|
Read line from f |
f.readlines()
|
Return list of lines in f |
f.write(s)
|
Write s to f |
f.close()
|
Close f |
In-built functions
int() , float() , str() , bool() ...
|
Type casting |
len(data)
|
Length |
min(values) , max(values)
|
Minimum / Maximum |
pow(x,y, [z])
|
X to the power Y [mod Z] |
range(start, stop, [step])
|
Ordered list |
input() , print()
|
Console Input/Output |
filter(function, array)
|
Filter array |
map(function, array)
|
Map function onto array |
id(object)
|
Unique object ID |
round(n, [x])
|
Round n [x decimal places] |
Module import
import module
|
from module import submodule
|
|
|
Control Flow
if(cond): <code> else: <code>
|
If-else |
if(cond1): <code> elif(cond1): <code> else: <code>
|
If-elif-else |
for i in range([start], stop, [step]): <code>
|
For loop over range |
for i in items: <code>
|
For loop over iterable |
while(condition): <code>
|
While loop |
break
|
Exit first enclosing loop |
continue
|
Skip to next iteration |
Useful standard library modules
math , numpy , scipy
|
Math |
matplotlib
|
Graph plotting |
random
|
Random generators |
datetime
|
Date and time |
timeit
|
Performance |
re
|
Regular expressions |
os
|
OS interaction |
sys
|
stdin, stdout, stderr, version |
urllib
|
Internet access |
zlib
|
Data compression |
Object-oriented
class Person:
|
Class definition |
x = Person(age, height)
|
Object creation |
x.age
|
Field access |
x.birthday()
|
Method access |
|
Created By
Metadata
Favourited By
Comments
good
Add a Comment
Related Cheat Sheets