Unit: Unit 5: Dictionaries and Sets • Interactive Practice
Interactive Python programming tutorial and practice in your 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}")