Python Reading
- Read the chapter entitled Dictionaries
from section "Dictionary Methods" through section "Aliasing and Copying"
Key Ideas
- Keys
- To retrieve all keys in a dictionary named inventory:
inventory.keys()
- To retrieve all keys as a list: list(inventory.keys())
- To retrieve keys one at a time: for key in inventory:
- To check if a key named "kiwi" exists: if "kiwi" in inventory:
- Values
- To retrieve all values: inventory.values()
- To retrieve a specific value, producing a runtime error if the
key is not present: inventory[key-name]
- To retrieve a specific value, producing NO runtime error if the
key is not present: inventory.get(key-name) or
inventory.get(key-name, value-to-return-if-key-not-present)
- Key-Value Pairs
- To retrieve all key-value pairs: inventory.items()
- Aliasing vs. Copying
- An example of an alias: dictionary1 = dictionary2
- The expression dictionary2 is dictionary1 returns True
- An example of a copy: dictionary2 = dictionary1.copy()
- The expression dictionary2 is dictionary1 returns False
Active Learning
- Work Exercise 16
- Work Exercise 19 without looking at the answer.
Feel free to add additional words to translate!