Practice Question 5
In this post, we will learn how to write a function that reads a text file and counts how many times each alphabet (a to z) appears. Instead of using multiple counters, we will use a Dictionary to store the frequency of every letter.
The Logic (How it works)
Before writing the code, let us first understand the logic behind the solution.
When we want to count occurrences of many different items, a Dictionary ({key: value}) is the best tool. Here is the teacher’s breakdown:
-
Open & Read: We open
article.txtand read the content into a string. -
Ignore Case Sensitivity: To treat ‘A’ and ‘a’ as the same, we convert the character to lowercase using
.lower(). -
Filter Alphabets: We check if the character is actually a letter using
.isalpha()so we don’t count spaces or numbers. -
The Dictionary Logic: If the letter is already present in the dictionary, increase its count.
If the letter is not present, add it with value 1.
Let’s Code as per the above logic :
def alphabetFrequency():
f = open("article.txt", "r")
data = f.read()
dic = {}
for char in data:
# Convert to lowercase to ignore case sensitivity
alpha = char.lower()
# We only want to count alphabets (a-z)
if alpha.isalpha():
if alpha in dic:
dic[alpha] += 1
else:
dic[alpha] = 1
print(dic)
f.close()
# To call the function:
alphabetFrequency()
Important Notes for Students
This logic is slightly more advanced, so keep these points in mind for your exams:
-
Why a Dictionary? A dictionary allows us to store the “Letter” as a Key and the “Count” as a Value. This is much faster and cleaner than creating 26 separate variables for each letter!
-
Case Sensitivity: By using
alpha = char.lower(), we ensure that ‘P’ and ‘p’ are counted together. If you forget this, your dictionary will have separate entries for ‘P’ and ‘p’. -
The
isalpha()Check: Without this check, your dictionary will also count spaces (), newlines (\n), and punctuation as characters. Always useisalpha()if the question specifically asks for “alphabets.”
Teacher’s Secret Tip for Exams:
- If the examiner asks you to count only vowels instead of all alphabets, you just need to change the condition from
isalpha()to checking'aeiou'. The rest of the program will remain exactly the same. - If the examiner asks you to treat uppercase and lowercase letters separately, simply remove the
.lower()conversion. This way,'A'and'a'will be counted as different characters. - If the examiner asks you to count the frequency of words instead of letters, you only need to split the file content using
split()and apply the same dictionary logic on each word. - If the examiner asks you to find the least frequent character, you can simply find the minimum value in your dictionary after the loop ends. The dictionary already stores all required data.
- If the examiner asks you to display the frequency in alphabetical order, you can sort the dictionary keys before printing them. The counting logic remains unchanged.
Want to practice more? If you found this helpful, I have compiled a full list of 30 practice problems just like this one. Each question focuses on a different logic to help you become a pro at File Handling.
Click here to view the full 30-Question Practice Set