Operators
Assignment |
= |
Arithmetic |
+, -, *, /, % |
Comparison |
>, >=, <, <=, ==, != |
Logical |
not, and, or |
Strings // saad = "Saad"
saad.count('a') |
returns 2 |
saad.index('d') |
returns 3 |
saad.index('a') |
returns 1 #first appearance |
saad.upper() |
returns "SAAD" |
saad.lower() |
returns 'saad' |
saad.isupper() |
returns False #checks if it is upper |
|
|
Built in functions
int(), float(), str(), |
Data type conversion |
len(object) |
returns length of object |
min(object), max(object) |
returns min, max |
range(start, stop, [step]) |
sequence object from 1 UP TO stop |
Break and Continue
break |
exists loop |
continue |
continues the loop |
|
|
Indexing // saad = [1,2,3,4,5]
saad[0] |
returns the first element (1) |
saad[-1] |
returns the last element (5) |
saad[:4] |
returns [1,2,3,4,5] |
saad[3:] |
returns [4,5] |
saad[::-1] |
returns [5,4,3,2,1] |
import random
random.range(x,y) |
returns random between x and y |
random.randint(x, y) |
returns random int between (x,y) |
random.choice(list) |
returns random element from list |
random.shuffle(list) |
returns shuffled list |
|
|
lists // saad[1, 2, 3, 4, 5]
saad.append(6) |
returns [1,2,3,4,5,6] |
saad.remove(3) |
returns [1, 2, 4, 5, 6] |
saad.insert(3,2) |
returns [1, 2, 3, 4, 5, 6] |
saad.sort() |
sort from lowest to highest |
|