Python notes

¡Supera tus tareas y exámenes ahora con Quizwiz!

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....


Conjuntos de estudio relacionados

Chapter 13: Essential Trace & Ultratrace Minerals

View Set

Chapter 4 Homework (Respiratory System)

View Set

health and illness concepts II- Ch. 1

View Set

20th Century Art History Part Two

View Set

AMH 2020 Final Exam Questions (Quiz 2)

View Set

Inflammation & Autoimmune Disorders NCLEX

View Set

LVN LEVEL III - FINAL EXAM - ANTEPARTUM

View Set

Chapter 1 The Scientific Method & Descriptive Research Methods

View Set

Histo Ch 4 Epithelial Tissue (Exam 1)

View Set