Skip to content

Week 4: Strings, lists, dictionaries, and sets

~9 min

This week's goal

This week you learn to work with groups of values. Strings are text, lists keep items in order, tuples are lists that cannot change, dictionaries map keys to values, and sets keep unique items. You also learn slicing and comprehensions, which build a new list in one line. By the end you can write a word counter that works on any text file.

The main text is Part 3 of the Python course, lessons 1 to 7.

Day-by-day (about 1.5-2 hours per day)

Day 1. Read strings. Practice len, .lower(), .strip(), .split(), and .join() in the interactive prompt. Notice that string methods return a new string and do not change the old one. Write clean.py that reads a sentence, removes the spaces at each end, and prints it in lower case.

Day 2. Read lists and tuples. Make a list of five numbers, then use append, len, sum, min, and max. Make a tuple with three values and try to change one of them. Read the error you get. Tuples cannot be changed, and that property is useful for values that should stay fixed.

Day 3. Read dictionaries. Build a dictionary that maps five city names to their populations, or any five items you like. Look up a key, add a new key, and loop over .items(). Note that a dictionary can be printed in insertion order, which the tutorial mentions. Do not rely on the order for logic unless you sort it.

Day 4. Read sets and slicing. A set removes duplicates, and in checks membership quickly. Slicing takes part of a sequence with start:stop, where the stop is not included. Try "hello"[1:4] and [10, 20, 30, 40][::2] in the prompt, and write down what you expect before you run them.

Day 5. Read comprehensions. A list comprehension such as [x * x for x in range(5)] builds a list in one line. Rewrite a loop from an earlier file as a comprehension, and check that the output is the same. Stop before the code becomes hard to read. A plain loop is often the better choice.

Day 6. Write word_count.py. It reads a text file that you name with input, splits it into lower-case words, removes the punctuation at each end of a word with strip, and counts each word in a dictionary. Print the five most common words with sorted. Use a small text file of your own, a few paragraphs long, to test it.

Day 7 is a rest day.

Deliverable

word_count.py reads a text file and prints the five most common words with their counts. Test it on two files of different length, and check the count for one word by hand. Your notes should say what you did when a word had punctuation at the end.

Check yourself

  1. Why does s.upper() not change s when you call it?
  2. What is the difference between a list and a tuple?
  3. What does d["x"] do when the key "x" is not in the dictionary?
  4. What does "python"[0:3] return?
  5. What does a set do with the value 3 when it already contains 3?
Answers
  1. Strings in Python cannot be changed. The method returns a new string. To keep the result, assign it, for example s = s.upper().
  2. Both hold an ordered collection. A list can change, so you can add and remove items. A tuple cannot change after you create it.
  3. It raises a KeyError. Use d.get("x") if you want None when the key is missing.
  4. It returns "pyt". The slice starts at index 0 and stops before index 3.
  5. Nothing changes. A set keeps only one copy of each value.

Sources