text = """Python is easy. Python is powerful.
Learning Python is fun, and fun is important!"""
words = [w.strip(".,!?").lower() for w in text.split()]
counts = {}
for w in words:
counts[w] = counts.get(w, 0) + 1
top = max(counts, key=counts.get)
print("Total words:", len(words))
print("Unique words:", len(counts))
print("Most common:", top, counts[top])
Output Verified by running it on Python 3
Total words: 14
Unique words: 8
Most common: is 4
split() also splits across the newline. max(counts, key=counts.get) finds the key with the largest count; "is" (4) beats "python" (3).
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!