Python Learning
5.74K subscribers
582 photos
2 videos
106 files
130 links
Python learning resources

Beginner to advanced Python guides, cheatsheets, books and projects.

For data science, backend and automation.
Join 👉 https://rebrand.ly/bigdatachannels

DMCA: @disclosure_bds
Contact: @mldatascientist
Download Telegram
🐍 Python Beginner Notes
❤4👏2
Python Set Methods ✍️
❤2👏1
🐍 Python Performance Optimization

Python Performance Optimization: Make Your Code Faster
Writing Python code that works is only the beginning. For real-world applications, performance matters.

Here are some techniques that can significantly improve Python performance:

⚡️ 1. Use the right data structures
Choosing a set instead of a list for frequent membership checks can dramatically reduce lookup time.
⚡️ 2. Avoid unnecessary loops
Use built-in functions, comprehensions, and optimized libraries such as NumPy when appropriate.
⚡️ 3. Profile before optimizing
Tools like cProfile and timeit help identify the actual bottlenecks instead of optimizing blindly.
⚡️ 4. Reduce unnecessary memory usage
Generators can process large datasets without loading everything into memory at once.
⚡️ 5. Use vectorization for data processing
NumPy operations can be much faster than manually looping through millions of values.

💡 Key principle:
Don't optimize what you haven't measured.
❤2
🐛 This Python Bug Looks Random, But It Isn't

Look at this:
functions = []

for i in range(3):
functions.append(lambda: i)

for f in functions:
print(f())


What would you expect?
0
1
2

Actual output:
2
2
2


Why? The lambdas don't store the current value of i. They remember the variable i.
By the time the functions are called, the loop has finished and i is 2.

This is called late binding.
If you actually want each function to capture the current value:
functions = []

for i in range(3):
functions.append(lambda i=i: i)

Now:
0
1
2


This becomes particularly important when creating callbacks inside loops, especially in GUI code, asynchronous code, and event-driven applications.

The bug isn't in lambda. It's in understanding when the variable is looked up.
❤1