Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Word frequency analysis in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Word Frequency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of word frequency count using Counter
What is the output of this Python code that counts word frequencies in a list?
Data Analysis Python
from collections import Counter
words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
freq = Counter(words)
print(freq)
ACounter({'apple': 3, 'banana': 2, 'orange': 1})
B{'apple': 3, 'banana': 2, 'orange': 1}
CCounter({'apple': 2, 'banana': 2, 'orange': 2})
D{'apple': 2, 'banana': 2, 'orange': 1}
Attempts:
2 left
💡 Hint
Remember that Counter counts how many times each word appears.
❓ data_output
intermediate
1:30remaining
Number of unique words in a text
Given this list of words, how many unique words are there?
Data Analysis Python
words = ['data', 'science', 'data', 'analysis', 'science', 'python', 'python', 'python']
unique_words = len(set(words))
print(unique_words)
A8
B3
C5
D4
Attempts:
2 left
💡 Hint
Use set to find unique items in a list.
❓ visualization
advanced
2:30remaining
Bar chart of word frequencies
Which option shows the correct bar chart for the word frequencies in this data?
Data Analysis Python
import matplotlib.pyplot as plt
from collections import Counter
words = ['cat', 'dog', 'cat', 'bird', 'dog', 'dog']
counter = Counter(words)
plt.bar(counter.keys(), counter.values())
plt.show()
ABar chart with bars: cat=3, dog=2, bird=1
BBar chart with bars: cat=2, dog=3, bird=1
CBar chart with bars: cat=1, dog=2, bird=3
DBar chart with bars: cat=2, dog=2, bird=2
Attempts:
2 left
💡 Hint
Count how many times each word appears before plotting.
🔧 Debug
advanced
2:00remaining
Identify the error in word frequency code
What error does this code raise when counting word frequencies?
Data Analysis Python
words = ['red', 'blue', 'red', 'green']
freq = {}
for word in words:
    freq[word] += 1
print(freq)
ASyntaxError
BTypeError
CKeyError
DNo error, prints correct counts
Attempts:
2 left
💡 Hint
Check what happens when you try to add to a dictionary key that does not exist yet.
🚀 Application
expert
3:00remaining
Find the most frequent word in a large text
Given a large text split into words, which code snippet correctly finds the most frequent word?
Data Analysis Python
text = 'data science is fun and data science is powerful and data is everywhere'
words = text.split()
A
from collections import Counter
counter = Counter(words)
most_common = counter.most_common(1)[0][0]
print(most_common)
B
freq = {}
for w in words:
    freq[w] = freq.get(w, 0) + 1
most_common = max(freq)
print(most_common)
C
from collections import Counter
counter = Counter(words)
most_common = max(counter)
print(most_common)
D
freq = {}
for w in words:
    freq[w] = freq.get(w, 0) + 1
most_common = max(freq.values())
print(most_common)
Attempts:
2 left
💡 Hint
Use Counter's most_common method to get the word with highest count.

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

  1. Step 1: Understand the purpose of word frequency analysis

    Word frequency analysis counts how often each word appears in a text.
  2. Step 2: Compare options with the purpose

    Only How many times each word appears in the text matches this purpose exactly.
  3. Final Answer:

    How many times each word appears in the text -> Option A
  4. 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

  1. Step 1: Recall the correct import syntax for Counter

    The correct way is 'from collections import Counter'.
  2. Step 2: Check each option's syntax

    Only from collections import Counter uses valid Python import syntax and correct name.
  3. Final Answer:

    from collections import Counter -> Option D
  4. Quick Check:

    Correct import = from collections import Counter [OK]
Hint: Remember 'from collections import Counter' exactly [OK]
Common Mistakes:
  • Using wrong import order
  • Trying to import non-existent modules
  • Using invalid syntax like 'import Counter from'
3.

What is the output of this code?

from collections import Counter
text = "apple banana apple orange banana apple"
words = text.split()
count = Counter(words)
print(count)
medium
A. Counter({'apple': 1, 'banana': 1, 'orange': 1})
B. {'apple': 3, 'banana': 2, 'orange': 1}
C. Counter({'apple': 3, 'banana': 2, 'orange': 1})
D. {'apple': 1, 'banana': 2, 'orange': 3}

Solution

  1. Step 1: Split the text into words

    The text splits into ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'].
  2. Step 2: Count each word using Counter

    'apple' appears 3 times, 'banana' 2 times, 'orange' 1 time.
  3. Final Answer:

    Counter({'apple': 3, 'banana': 2, 'orange': 1}) -> Option C
  4. 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

  1. Step 1: Identify the type of 'words'

    words is a list of strings after split().
  2. Step 2: Check the use of lower()

    Calling words.lower() is invalid because lists have no lower() method; lower() applies to strings.
  3. Final Answer:

    Cannot call lower() on a list; should call on each word -> Option A
  4. 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

  1. Step 1: Understand the need to remove punctuation and lowercase

    To count words ignoring case and punctuation, text must be lowercased and punctuation removed.
  2. 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.
  3. Final Answer:

    Code that lowercases and removes punctuation before counting -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard