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. 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 in my_dict for key "blah", it will create one, with value 6. If there is, it will change the value for that entry to 6.
  • len(my_dict): Outputs the number of distinct keys in my_dict.
  • del my_dict["blah"]: Deletes the entry in my_dict for key "blah"
  • for key in dict: Will iterate through all the keys in my_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 in my_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 python dictpract.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:

  1. Read the example code
  2. Write a prediction for what it will do
  3. Press Enter in the Shell
  4. 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.

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 of s, and returns what’s left. Note that whitespace is not removed from the interior of s. Only the outsides.
  • s.strip(t), where t is another string: Removes all instances of any character in t from the beginning and end of s. Note that order doesn’t matter here. Python just keeps deleting stuff from the front and back of s as long as it’s a letter in t.
  • s.split(): Returns a list of “words” in s, i.e. the sub-strings of s that are separated by whitespace characters.
  • s.split(t), where t is another string: Returns a list of “words” in s, but instead of looking for sub-strings separated by spaces, Python divides s whenever it encounters an instance of t. Unlike with s.strip(), order matters here – we’re not looking for any letter in t, but rather all of them, in the right order.
  • s.upper(): Returns a copy of s with all the letters uppercase.
  • s.lower(): Returns a copy of s with all the letters lowercase.
  • string.punctuation: Shorthand for the string !"#$%&'()*+, -./:;<=>?@[\]^_`{|}~. Note that this variable can only be accessed when we import the string module.