Count frequency of words in a sentence Dry Run in PYTHON

Count frequency of words in a sentence is an interactive PYTHON dry run visualizer from the Dict programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Count frequency of words in a sentence Program Code

# Count frequency of words in a sentence
sentence = "the quick brown fox jumps over the lazy dog the quick brown fox"
print("Sentence:", sentence)

# Split into words and count
words = sentence.split()
word_freq = {}
for word in words:
    word_freq[word] = word_freq.get(word, 0) + 1

print("\nWord frequencies:", word_freq)

# Display sorted by word
print("\nSorted by word:")
for word in sorted(word_freq.keys()):
    print(f"  {word}: {word_freq[word]}")

# Display sorted by frequency (most common first)
print("\nSorted by frequency (most common):")
for word, count in sorted(word_freq.items(), key=lambda x: x[1], reverse=True):
    print(f"  {word}: {count}")

# Find most common word
most_common = max(word_freq, key=word_freq.get)
print(f"\nMost common word: '{most_common}' appears {word_freq[most_common]} times")

View the complete Dict programs page.

Program Console Count frequency of words in a sentence Topic: PYTHON Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.