Lab 10 Dictionaries
Summary
Practice with Python dictionaries
Lab Setup
- Find your partner
- Download lab10.zip which includes the starter code for today's exercises
- Extract that zip file and move the resulting
lab10
folder into yourcs111
directory on your desktop. - Open Thonny and view the
lab10
files
Pair Programming Tips
1. Recognize that pairing is hard
Pairing is an intense relationship, summoning all your powers of ingenuity, communication, humour, and code. If you find it hard, or even just tiring, it's because it is hard and tiring sometimes. It takes effort to be kind, patient and to help steer someone in the right direction (or to be willing to be steered). Respect your partner and be kind. Your job is to help each other learn.
2. Be mindful of your body language
Is your back to your partner? Is your computer screen angled away? Do you make eye contact? You want to create an atmosphere of inclusion so your pair feels respected and encouraged to comment.
3. Is it too quiet between you and your partner?
If there is a lot of silence, that is often a sign that someone feels left behind. Did you take over the keyboard and write a bunch of lines in silence? Yikes! Explain what you type to your partner. Or perhaps you are confused but feel uncomfortable speaking up. Try: "I don't understand this line, can you explain what it does?"
(These tips are from this blog post on pair programming)
Table of Contents
- Lab 10 Home
- Cheat Sheet: Lists & Dictionaries
- Part 1: Exercises
- Part 2: Ironman data
- Knowledge Check
Big Questions
- Why would you want to use a dictionary of key/value pairs instead of just a
list of key/value tuples?
Show Answer
The main advantage of a dictionary over a list is that you can quickly look up values according to their key. So any situation where you want to be able to look things up not by order (lists are fine if you just want to sort things) but arbitrarily by an associated value, a dictionary makes sense. For example, a dictionary with state names as keys and state capitol names as values would let you look up the capitol city of any state quickly, whereas doing that with a list would be less convenient. - Why are tuples useful in the context of dictionaries?
Show Answer
For most things, a list can simply be used instead of a tuple. But because dictionaries require their keys to be immutable, a list cannot be used as the key of a dictionary, whereas a tuple can be.