Python Learning
🐍 Python’s Secret Memory Saver: __slots__ ⚡️
👉 Most Python tutorials teach you Object-Oriented Programming (OOP) using self.variable = value. But almost none mention what happens under the hood or how it can quietly eat up your RAM.
When you create thousands or millions of object instances, Python’s default behavior wastes a massive amount of memory. Here is how __slots__ fixes that.
——————————
🔹 1. The Hidden Problem with Default Python Classes
By default, Python stores an object's attributes in a dynamic dictionary called __dict__.
👉 Why this is a problem:
❌ Dictionaries are flexible, but extremely memory-heavy.
❌ Every single instance gets its own dictionary overhead.
❌ If you instantiate 100,000 objects, your application’s RAM usage skyrockets.
——————————
🔥 2. The Solution: __slots__
__slots__ tells Python:
Do not create a dynamic __dict__ for this class. Only allow these specific attribute names.
——————————
🔹 3. Standard Class vs. Slotted Class
❌ Standard Class (Uses Heavy __dict__):
class DataPoint:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
✅ Optimized Class with __slots__:
class DataPoint:
# Restrict attributes & eliminate __dict__
__slots__ = ("x", "y", "z")
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
——————————
📊 4. The Real-World Impact
By adding that single line of code (__slots__):
✔️ ~60% to 70% reduction in memory usage across large object lists.
✔️ Faster attribute access (up to 20% faster speed because Python skips dictionary lookups).
——————————
⚠️ 5. The Trade-Off (What You Must Know)
Because __slots__ locks down your object structure:
❌ You cannot dynamically add new attributes at runtime (e.g., point.new_var = 10 will throw an AttributeError).
——————————
❔ 6. When Should You Use It?
✔️ Working with huge datasets or simulation objects in memory.
✔️ Building high-performance backend microservices.
✔️ De
4 · 458 · Python Learning
Topic: Python
🔍 Quick look before the question:
def outer():
x = 10
def inner():
nonlocal x
x += 5
return x
return inner
f = outer()
print(f())
print(f())
1 · 453 · Python Learning
Файл
Python for Data Science Cheat Sheet.pdf · 372 КБ · нажмите — покажем
Python for Data Science Cheat Sheet.pdf · 372 КБ · нажмите — покажем
Python for Data Science Cheatsheet
8 · 476 · Python Learning
🐍 Python’s Hidden Loop Feature: for...else
👉 Did you know else isn't just for if statements?
Python has a unique feature almost never mentioned in beginner tutorials: you can attach an else block directly to a for or while loop.
🔹 How It Works
The else block executes ONLY if the loop finishes completely without hitting a break statement.
🔹 The Difference
❌ Traditional Way (Requires a messy flag variable):
found = False
for user in users:
if user == "Alex":
found = True
break
if not found:
print("User not found!")
✅ Pythonic Way (Using for...else):
for user in users:
if user == "Alex":
print("User found!")
break
else:
print("User not found!")
🔹 Why Use It?
✔️ Eliminates unnecessary boolean flags (like found = True).
✔️ Cleaner, more readable syntax for search functions.
3 · 483 · Python Learning
⚡️ Python’s 1-Line Speed Booster: @lru_cache
👉 Did you know you can make slow Python functions run up to 100x faster by adding a single line of code?
Most tutorials skip functools.lru_cache, but it’s one of Python’s best built-in performance hacks.
🔹 How It Works
It automatically caches (remembers) the results of function calls. If you call the function with the same inputs again, Python skips the heavy computation and returns the saved answer instantly.
🔹 Code Comparison
❌ Slow (Re-calculates every single call):
def get_user_data(user_id):
# Imagine an expensive database query here
return fetch_from_db(user_id)
✅ Very Fast (Remembers previous results):
from functools import lru_cache
@lru_cache(maxsize=128)
def get_user_data(user_id):
# Only runs ONCE per unique user_id
return fetch_from_db(user_id)
🔹 Use It to:
✔️ Speedup repetitive API calls, math calculations, or DB queries.
✔️ No third-party libraries needed (built into Python's standard library).
✔️ Prevent unnecessary server load.
3 · 365 · Python Learning
25 Github Repositories Every Python Developer Should Know
1. Python
The official repository of Python's source code. Dive into it to explore Python's internals or contribute to the language's development.
2. Awesome Python
A curated list of awesome Python frameworks, libraries, software, and resources. A perfect starting point for any Python developer.
3. Requests
Simplifies HTTP requests in Python. A must-have library for working with APIs and web scraping.
4. Flask
A lightweight web framework that is simple to use yet highly flexible, ideal for small to medium-sized applications.
5. Django
A high-level web framework that encourages rapid development and clean, pragmatic design for building robust web applications.
6. FastAPI
A modern web framework for building APIs with Python. Known for its speed and automatic OpenAPI documentation.
7. Pandas
Provides powerful tools for data manipulation and analysis, including support for data frames.
8. NumPy
The go-to library for numerical computations. It’s the backbone of Python’s scientific computing stack.
9. Matplotlib
A plotting library for creating static, animated, and interactive visualizations in Python.
10. Seaborn
Builds on Matplotlib and simplifies creating beautiful and informative statistical graphics.
11. Scikit-learn
A machine learning library featuring various classification, regression, and clustering algorithms.
12. TensorFlow
A powerful framework for machine learning and deep learning, supported by Google.
13. PyTorch
Another leading machine learning framework, known for its flexibility and dynamic computation graph.
14. BeautifulSoup
Simplifies web scraping by parsing HTML and XML documents.
15. Scrapy
An advanced web scraping and web crawling framework.
16. Streamlit
Makes it easy to build and share data apps using pure Python. Great for data scientists.
17. Celery
A distributed task queue library for running asynchronous jobs.
18. SQLAlchemy
A powerful ORM (Object-Relational Mapping) t
74 · 2.3K · Python Learning
Фотография
нажмите — покажем
нажмите — покажем
The tell() function in Python 🐍
The tell() function returns the current position of the file pointer within the data stream. It is most often used when working with files. 📂
The function does not accept any arguments and returns an integer - the position in bytes from the beginning of the stream. 🔢
with open("file.txt", "rb") as f:
print(f.tell())
3 · 293 · Python Learning
PYTHON SKILL ROADMAP
│
├── 📁 Python Basics
│ ├── 📁 Variables & Data Types
│ ├── 📁 Input & Output
│ ├── 📁 Operators
│ ├── 📁 Conditional Statements
│ └── 📁 Loops
│
├── 📁 Core Python Concepts
│ ├── 📁 Lists
│ ├── 📁 Tuples
│ ├── 📁 Sets
│ ├── 📁 Dictionaries
│ ├── 📁 Strings
│ └── 📁 Functions
│
├── 📁 Problem Solving
│ ├── 📁 Patterns
│ ├── 📁 Number Problems
│ ├── 📁 String Problems
│ ├── 📁 List Problems
│ ├── 📁 Searching
│ └── 📁 Sorting Basics
│
├── 📁 Object-Oriented Python
│ ├── 📁 Classes & Objects
│ ├── 📁 Constructors
│ ├── 📁 Inheritance
│ ├── 📁 Encapsulation
│ ├── 📁 Polymorphism
│ └── 📁 Real OOP Examples
│
├── 📁 File Handling & Errors
│ ├── 📁 Read Files
│ ├── 📁 Write Files
│ ├── 📁 CSV Files
│ ├── 📁 JSON Files
│ ├── 📁 Exception Handling
│ └── 📁 Logging Basics
│
├── 📁 Python Libraries
│ ├── 📁 NumPy Basics
│ ├── 📁 Pandas Basics
│ ├── 📁 Matplotlib Basics
│ ├── 📁 Requests
│ ├── 📁 BeautifulSoup
│ └── 📁 Streamlit Basics
│
├── 📁 Automation Skills
│ ├── 📁 File Organizer
│ ├── 📁 Email Automation
│ ├── 📁 Web Scraping
│ ├── 📁 API Automation
│ ├── 📁 Excel Automation
│ └── 📁 Task Scheduler
│
├── 📁 Backend Basics
│ ├── 📁 Flask Basics
│ ├── 📁 FastAPI Basics
│ ├── 📁 REST APIs
│ ├── 📁 Databases
│ ├── 📁 Authentication Basics
│ └── 📁 Deploy Your API
│
└── 📁 Portfolio Projects
├── 📁 Expense Tracker
├── 📁 Weather App
├── 📁 Web Scraper
├── 📁 URL Shortener
├── 📁 Automation Bot
└── 📁 AI Note Summarizer
Learn the syntax first.
Then solve problems.
Then build projects.
That is how Python starts making sense.
@python_bds
4 · 290 · Python Learning
Фотография
нажмите — покажем
нажмите — покажем
🧠 return vs print() in Python
These are not interchangeable.
def add(a, b):
print(a + b)
Calling:
result = add(2, 3)
prints:
5
But:
result
is actually:
None
Now compare:
def add(a, b):
return a + b
This time:
result = add(2, 3)
gives:
result == 5
print() sends something to the screen.
return sends a value back to the caller.
That distinction becomes extremely important once functions start calling other functions.
2 · 254 · Python Learning
Фотография
нажмите — покажем
нажмите — покажем
Фотография
нажмите — покажем
нажмите — покажем
🐍 Python Beginner Notes
7 · 553 · Фотография
нажмите — покажем
нажмите — покажем
Фотография
нажмите — покажем
нажмите — покажем
Фотография
нажмите — покажем
7 · 594 · нажмите — покажем
Фотография
нажмите — покажем
нажмите — покажем
Фотография
нажмите — покажем
нажмите — покажем
Фотография
нажмите — покажем
нажмите — покажем
Фотография
нажмите — покажем
нажмите — покажем
Фотография
нажмите — покажем
7 · 603 · нажмите — покажем