Friday, 26 June 2026

πŸš€ Day 76/150 – Count Word Frequency in Python

 


πŸš€ Day 76/150 – Count Word Frequency in Python

Counting the frequency of words is a common task in text analysis, data processing, and natural language processing (NLP). It helps us determine how many times each word appears in a sentence or paragraph.

Let's explore different ways to count word frequencies in Python.


πŸ”Ή Method 1 – Using Dictionary

The most fundamental approach is to use a dictionary.

text = "python is easy and python is powerful" words = text.split() freq = {} for word in words: freq[word] = freq.get(word, 0) + 1 print(freq)





Output

{
'python': 2,
'is': 2,
'easy': 1,
'and': 1,
'powerful': 1
}

Explanation

  • split() converts the sentence into a list of words.
  • get(word, 0) returns the current count or 0 if the word doesn't exist.
  • Each occurrence increases the count by 1.

πŸ”Ή Method 2 – Taking User Input

text = input("Enter a sentence: ") words = text.split() freq = {} for word in words: freq[word] = freq.get(word, 0) + 1 print(freq)








Example Input
hello world hello python

Output

{
'hello': 2,
'world': 1,
'python': 1
}
πŸ”Ή Method 3 – Using count()


A simple approach for small datasets.


text
= "python is easy and python is powerful" words = text.split() for word in set(words): print(word, ":", words.count(word))







Output
python : 2
is : 2
easy : 1
and : 1
powerful : 1



Explanation
  • set(words) removes duplicates.
  • count() counts occurrences of each word.
  • Less efficient for large texts because it scans the list repeatedly.

πŸ”Ή Method 4 – Using Function

def word_frequency(text): freq = {} for word in text.split(): freq[word] = freq.get(word, 0) + 1 return freq print(word_frequency("python is easy and python is powerful"))










Output
{
'python': 2,
'is': 2,
'easy': 1,
'and': 1,
'powerful': 1
}


Explanation

  • Encapsulates the logic inside a reusable function.
  • Makes the code cleaner and easier to maintain.

    πŸ”₯ Key Takeaways

    ✔️ Use a dictionary for learning the logic behind frequency counting.

    ✔️ get() helps avoid checking whether a key exists.

    ✔️ Counter is the most efficient and Pythonic solution.

    ✔️ Word frequency analysis is widely used in data science and NLP.

    ✔️ split() is commonly used to convert text into words.




0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (291) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (30) Azure (11) BI (10) Books (262) Bootcamp (11) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (32) data (6) Data Analysis (37) Data Analytics (25) data management (16) Data Science (374) Data Strucures (22) Deep Learning (183) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (21) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (74) Git (12) Google (53) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (42) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (325) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (14) PHP (20) Projects (34) Python (1388) Python Coding Challenge (1169) Python Mathematics (1) Python Mistakes (51) Python Quiz (551) Python Tips (18) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (20) SQL (52) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)