📖 Scenario: Imagine you have a short paragraph of text from a book or an article. You want to find out how many times each word appears in that text. This is useful for understanding which words are used most often.
🎯 Goal: You will create a program that counts how many times each word appears in a given text and then shows the counts.
📋 What You'll Learn
Create a dictionary with words as keys and their counts as values
Use a variable to hold the text to analyze
Use a loop to count the words
Print the final word counts
💡 Why This Matters
🌍 Real World
Word frequency analysis helps in understanding text data, like finding popular topics or keywords in articles, social media, or customer reviews.
💼 Career
Data scientists and analysts use word frequency counts to prepare text data for further analysis or machine learning.
Progress0 / 4 steps
1
Create the text data
Create a variable called text and set it to the exact string: 'Data science is fun and data is powerful'
Data Analysis Python
Hint
Use single or double quotes to create the string exactly as shown.
2
Prepare the word list
Create a variable called words that contains the list of words from text by splitting text on spaces using text.split()
Data Analysis Python
Hint
Use the split() method on the text variable without any arguments.
3
Count the words
Create an empty dictionary called word_counts. Then use a for loop with the variable word to go through each word in words. Inside the loop, add 1 to the count for each word in word_counts. If the word is not in the dictionary, add it with count 1.
Data Analysis Python
Hint
Use dict.get(key, 0) to get the current count or 0 if the word is new.
4
Display the word counts
Write a print statement to display the word_counts dictionary.
Data Analysis Python
Hint
Use print(word_counts) to show the dictionary.
Practice
(1/5)
1.
What does word frequency analysis help us understand about a text?
easy
A. How many times each word appears in the text
B. The total number of characters in the text
C. The number of sentences in the text
D. The length of the longest word in the text
Solution
Step 1: Understand the purpose of word frequency analysis
Word frequency analysis counts how often each word appears in a text.
Step 2: Compare options with the purpose
Only How many times each word appears in the text matches this purpose exactly.
Final Answer:
How many times each word appears in the text -> Option A
Quick Check:
Word frequency = count of each word [OK]
Hint: Focus on counting words, not characters or sentences [OK]
Common Mistakes:
Confusing word count with character count
Thinking it counts sentences
Assuming it finds longest word length
2.
Which Python code correctly imports the tool to count word frequencies?
?
easy
A. import wordcount from collections
B. import Counter from collections
C. from collections import wordfreq
D. from collections import Counter
Solution
Step 1: Recall the correct import syntax for Counter
The correct way is 'from collections import Counter'.
Step 2: Check each option's syntax
Only from collections import Counter uses valid Python import syntax and correct name.
Final Answer:
from collections import Counter -> Option D
Quick Check:
Correct import = from collections import Counter [OK]
Counter({'apple': 3, 'banana': 2, 'orange': 1}) -> Option C
Quick Check:
Count matches word occurrences [OK]
Hint: Count words carefully; Counter shows counts with 'Counter()' [OK]
Common Mistakes:
Ignoring Counter output format
Miscounting word frequencies
Confusing dictionary and Counter print styles
4.
Find the error in this code that tries to count words:
from collections import Counter
text = "Hello world! Hello"
words = text.split()
count = Counter(words.lower())
print(count)
medium
A. Cannot call lower() on a list; should call on each word
B. Missing import statement for Counter
C. split() should be split(' ')
D. Counter cannot count strings
Solution
Step 1: Identify the type of 'words'
words is a list of strings after split().
Step 2: Check the use of lower()
Calling words.lower() is invalid because lists have no lower() method; lower() applies to strings.
Final Answer:
Cannot call lower() on a list; should call on each word -> Option A
Quick Check:
lower() works on strings, not lists [OK]
Hint: Apply lower() to each word, not the whole list [OK]
Common Mistakes:
Calling lower() on list instead of string
Ignoring error messages
Thinking split() needs arguments
5.
You want to count words ignoring case and punctuation in this text:
text = "Hello, hello! How are you? Are you fine?"
Which code correctly cleans and counts words?
hard
A. import re
from collections import Counter
words = re.findall(r'\w+', text)
count = Counter(words)
print(count)
B. import string
from collections import Counter
clean_text = text.lower().translate(str.maketrans('', '', string.punctuation))
words = clean_text.split()
count = Counter(words)
print(count)
C. from collections import Counter
words = text.split()
count = Counter(words)
print(count)
D. from collections import Counter
words = text.lower().split()
count = Counter(words)
print(count)
Solution
Step 1: Understand the need to remove punctuation and lowercase
To count words ignoring case and punctuation, text must be lowercased and punctuation removed.
Step 2: Check each option's approach
import string
from collections import Counter
clean_text = text.lower().translate(str.maketrans('', '', string.punctuation))
words = clean_text.split()
count = Counter(words)
print(count) uses string.punctuation with translate() to remove punctuation and lowercases text before splitting, which is correct.
Final Answer:
Code that lowercases and removes punctuation before counting -> Option B
Quick Check:
Clean text + Counter = accurate word frequency [OK]
Hint: Remove punctuation and lowercase before splitting words [OK]
Common Mistakes:
Not removing punctuation
Counting words with mixed cases separately
Using split() without cleaning text
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.