BeginnerCoder

Lesson 20 — Mini project: word counter

Put it together

This project combines three tools you know: split() breaks the text into words, a loop visits each word, and a dict remembers a count per word. The trick is counts.get(w, 0): it returns the current count, or 0 if the word has not been seen yet. Add one to that and store it back under the word.

votes = {}
votes["yes"] = votes.get("yes", 0) + 1
print(votes)  # {'yes': 1}

Why it matters: counting things is the start of almost every analysis — popular products, common errors, top search terms.

Want it another way?
Task

Count each word in the text and print the counts dict.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Python Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.