Python notes
if-else
a= True if a != b or not a: print("yes") a = 7 elif b == 7: print("no") else: print("bing") ------------------- for i in range(1,10,3) #prints 1-10, every 3 print (i) #third argument is the step ----------- for i in "abcdef" #prints everything but when it print( i, end=" ") #gets to end its empty ------------- try: c=10/0 except(TypeError): print("type") except: #no parenthesis here print("nope")
Classes, objects
add attributes to objects defining a class inheritance magic methods(___init___, ___str____)
lists
not immutable, can be changed
Class
class Thing: def __init__(self): #use magic methods to self.top= [1,2,3] #create new objects def __str__(self): return str(self.top[0]) a = Thing() b = Thing() a.top[0] = 99 print(b.top) #prints [1,2,3]
Defining a function
def f(): return 3 --------------- def g(a,b): return a*b
Control
indentation for blocks no braces(care about indentation) elif (short for else if ) range() for for for over iterable (iterates over any iterable) try, raise, except
__str__
kind of like the string method in java, whenever a string method is invoked on our class it will return this.
dict
like a hashmap, has key-value pairs keys can be different types even g[0]= "a" (key =0 , value="a") g["llama"]= "c" would give list(g) would give [0:= 'a' , 'llama' : 'c'] (llama = key, value = c)
Containers
str, tuple( immutable(can't change them)) list (can change) dict(indexed) (can change) conversion (can convert between them)
Python Data types
text : str numeric types: int, float, complex, sequence types: list, tuple, range mapping types: dict(kind of like hashmap) set types: set, frozenset boolean types: bool binary types: byte....