Part 6 of 6 · Chapter 4 of 4

Building Something From All of It

Every chapter until now has proven one idea in isolation. Build one program that needs variables, a loop, a function, and a dictionary all in the same twenty lines, and watch them stop being separate topics.

Advanced14 min read

Every chapter until now has proven one idea in isolation. Build one program that needs variables, a loop, a function, and a dictionary all in the same twenty lines, and watch them stop being separate topics.

Planning the shape before writing a line

The project: a command-line word-frequency counter. Give it a sentence, and it reports how many times each word appears — the smallest program that needs a loop, a dictionary, and a function working together rather than in isolation.

  1. 1

    Split the text into words

    A string method turns one long sentence into a list of individual words.

  2. 2

    Count each word with a dictionary

    Loop over the words, using each one as a key and a running count as its value.

  3. 3

    Wrap it in a function

    word_counts(text) takes a sentence in and returns the finished dictionary out.

  4. 4

    Test it before trusting it

    One normal sentence, and one edge case — an empty string — each with an assertion.

Building it piece by piece, testing each one

Terminal
$ >>> text = "the cat sat on the mat the cat ran"$ >>> words = text.split()$ >>> words$ ['the', 'cat', 'sat', 'on', 'the', 'mat', 'the', 'cat', 'ran']

split() with no argument breaks on any run of whitespace — the comprehensions and loops chapters both used exactly this kind of list as their starting point.

word_count.py
def word_counts(text):    counts = {}    for word in text.split():        counts[word] = counts.get(word, 0) + 1    return counts

counts.get(word, 0) returns the running total if word has been seen before, or 0 if this is its first appearance — the same dictionary-with-a-default pattern from the dictionaries chapter, now doing real work.

Where to go from here

Nothing about this program is specific to counting words — the same shape, a loop filling a dictionary, is the core of a shopping cart total, a vote tally, or a log file summary. The twenty-four chapters behind this one are not separate tools; they are the vocabulary this one program was written in.

Before calling this finished

  • word_counts(text) returns a dictionary, not a printed string — printing is a separate, later step.
  • An empty string produces an empty dictionary, not an error.
  • Punctuation attached to a word ("cat," versus "cat") is counted separately right now — worth noticing, even if you don't fix it yet.
  • Every function in the program has at least one assert proving what it claims to do.

Key takeaways

  • A real program combines ideas that were taught separately — this one needed a loop, a function, and a dictionary together, not in isolation.
  • counts.get(word, 0) is the dictionary-with-a-default pattern from earlier in the track, doing work here for the first time.
  • Testing the empty-string case here is the same habit the testing chapter argued for: prove the edge, not just the common case.
  • The shape of this program — loop, accumulate into a dict, wrap in a function — recurs constantly outside of word counting specifically.