




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































Programming Basics^ The Console Images Data structures Midterm Graphics Object-Oriented Programming Everyday Python
Today’s questions How can we store more information and add more structure to our data?
Today’s topics
● A key will only be associated with one value ○ no duplicate keys! ● A dictionary can have multiple values that are the same. dict ‘hansa’ ‘kandula’ ‘lumpy’ ‘surus’ 3 3 1 3 keys values
d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57} d.keys() dict_keys(['Gates', 'MemChu', 'Tresidder']) iterable collection of all the keys. iterable means it can be used in foreach
d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57} list(d.values()) [23, 116, 57] we are using list() to convert d.values() into a list
d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57} for building in d.keys(): ... print(building) Gates MemChu Tresidder we can use foreach on the dictionary’s keys!
d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57} for building, age in d.items(): ... print(building, ‘is’, age, ‘years old.’) Gates is 23 years old. MemChu is 116 years old. Tresidder is 57 years old. items() gives us key, value pairs
d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57} for building, age in d.items():
... print(building, age, sep=‘: ’) Gates: 23 MemChu: 116 Tresidder: 57 sep is an optional argument like end!
d = {‘Gates’: 23, ‘Tresidder’: 57, ‘MemChu’: 116} sorted(d.keys()) ['Gates', 'MemChu', 'Tresidder'] sorted() returns a list in alphabetical order!
d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57} min(d.values()) 23 max(d.values()) 116 returns the smallest element! returns the biggest element!
print() input() str() int() float() len() open() list() sorted() max() min()
Built-ins with Lists