Warmup
Part A: Dictionary Practice
This exercise will give you a little practice with dictionaries before you use them heavily in this week’s lab. Open up dictprac.py
, but don’t run the code yet. The program has a bunch of lines of code, which store movie ratings in a dictionary. It includes many print()
statements. Go through the file with your partner and predict what each print()
statement will do. You should write your predictions as comments directly in dictprac.py
. There is no WARMUP.md for lab 6 Consult the handy notes below for reminders on how dictionaries work. Once you have guesses, run the dictprac.py
file. The program will show you one line at a time, so that you can compare your predictions to the actual output. Press Enter to progress the program to the next line.
Dictionaries
Let my_dict
be your dictionary. Here are some things you can do with it.
my_dict["blah"] = 6
: If there is no entry inmy_dict
for key"blah"
, it will create one, with value6
. If there is, it will change the value for that entry to6
.len(my_dict)
: Outputs the number of distinct keys inmy_dict
.del my_dict["blah"]
: Deletes the entry inmy_dict
for key"blah"
for key in dict
: Will iterate through all the keys inmy_dict
. You can use this to look up the entries in sequence. Don’t make any assumptions about the order of the keys, though.my_dict.keys()
: This one’s tricky. It spits out a “view” of the keys inmy_dict
, which is a special data type. You can iterate through it like it was a list of keys, but if you print it, it will look weird.
README
The code in both Warmup files is “instrumented” so that you can run the file once and walk through the output example-by-example without spoilers. For example, when you run python3 dictprac.py
, Python will pause for 2 seconds in between each code snippet, wait for you to press Enter
, and then it will show the output. This way you can perform the following steps in order:
- Read the example code
- Write a prediction for what it will do
- Press
Enter
in the Shell - Compare your predicted output with the Python output
We suggest, however, that you predict all the code output before running the file for the first time.
Reminder
After you write out and test your predictions, be sure to commit and push your changes!
Part B: String Practice
This exercise will get you acquainted with the string methods that will be helpful for this week’s lab. Open up stringprac.py
, but don’t run the code yet. You’ll see a bunch of one-line commands. For each command, try with your partner to predict its output. Again, write your predictions as comments within stringprac.py
. You should use the handy notes below. Once you have guesses, run the stringprac.py
file. The program will show you one line at a time, so that you can compare your predictions to the actual output. Press Enter to progress the program to the next line.
Strings
Let s
be a string.
s.strip()
: Removes all whitespace (spaces and newline characters) from the beginning and end ofs
, and returns what’s left. Note that whitespace is not removed from the interior ofs
. Only the outsides.s.strip(t)
, wheret
is another string: Removes all instances of any character int
from the beginning and end ofs
. Note that order doesn’t matter here. Python just keeps deleting stuff from the front and back ofs
as long as it’s a letter int
.s.split()
: Returns a list of “words” ins
, i.e. the sub-strings ofs
that are separated by whitespace characters.s.split(t)
, wheret
is another string: Returns a list of “words” ins
, but instead of looking for sub-strings separated by spaces, Python dividess
whenever it encounters an instance oft
. Unlike withs.strip()
, order matters here – we’re not looking for any letter int
, but rather all of them, in the right order.s.upper()
: Returns a copy ofs
with all the letters uppercase.s.lower()
: Returns a copy ofs
with all the letters lowercase.string.punctuation
: Shorthand for the string!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
. Note that this variable can only be accessed when we import thestring
module.
Reminder
After you write out and test your predictions, be sure to commit and push your changes!