5.1.10 Practical Application: Frequency Counter

Unit: Unit 5: Dictionaries and Sets • Interactive Practice

Quick Answer & Summary:

Interactive Python programming tutorial and practice in your browser.

Interactive Challenge (Run Live in Browser):

# Count the frequency of words in a sentence
quote = "to be or not to be that is the question"
words = quote.split()

frequency_map = {}

# Build frequency table using the .get() accumulator pattern
for word in words:
    frequency_map[word] = frequency_map.get(word, 0) + 1

print("Word Frequencies:")
for word, count in sorted(frequency_map.items()):
    print(f"{word}: {count}")
Open in Live Interactive Python Sandbox →