π This Python Bug Looks Random, But It Isn't
Look at this:
What would you expect?
Actual output:
Why? The lambdas don't store the current value of
By the time the functions are called, the loop has finished and
This is called late binding.
If you actually want each function to capture the current value:
Now:
This becomes particularly important when creating callbacks inside loops, especially in GUI code, asynchronous code, and event-driven applications.
The bug isn't in
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.β€4
Forwarded from Free Programming Books
π The Big Book of Small Python Projects
βοΈ Author: Al Sweigart
π Read Online
#Python #Projects
βββββββββββ
π More free programming books β @free_programming_books_bds
Part of @bigdataspecialist community
βοΈ Author: Al Sweigart
π Read Online
#Python #Projects
βββββββββββ
π More free programming books β @free_programming_books_bds
Part of @bigdataspecialist community
β€3
π The most comprehensive Python cheat sheet! And it's free on GitHub
Basic datatypes, Generators & decorators, ALL important core modules, Threading, JSON & CSV, OOP, Typing, Logging, Exceptions, OS commands and soo much more!
π Comprehensive Python Cheatsheet
Basic datatypes, Generators & decorators, ALL important core modules, Threading, JSON & CSV, OOP, Typing, Logging, Exceptions, OS commands and soo much more!
π Comprehensive Python Cheatsheet
β€2
π Applied Machine Learning in Python
If you are getting started with MachineLearning or if you are an expert seeking a practical resource to refine your skills this e-book is for you.
β What you'll find inside:
1. Clear explanations of theory
2. Best practices for applied machine learning
3. Step-by-step demonstrations in Jupyter Notebooks
π You Can Read it Here
If you are getting started with MachineLearning or if you are an expert seeking a practical resource to refine your skills this e-book is for you.
β What you'll find inside:
1. Clear explanations of theory
2. Best practices for applied machine learning
3. Step-by-step demonstrations in Jupyter Notebooks
π You Can Read it Here
β€3
7 GitHub repositories that will help you become a great Python developer
1. Python Project Based Learning
β’ Curated list of project based tutorials
β’ 284k stars
β’ Webapps, web scraping, bots, data science, ML, and more.
2. All algorithms implemented in Python
β’ Boatloads of algorithms implemented in Python
β’ 225k stars
β’ Algorithms neatly categorized
3. 30 Days of Python
β’ Step-by-step guide to learn the Python
β’ 74k stars
β’ Logical learning sequence, great explanations and helpful getting started guide
4. 100-Days-Of-ML-Code
β’ A great resource for learning ML
β’ 52k stars
β’ High quality infographic explanations
5. Playground and Cheatsheet for Learning Python
β’ Collection of Python scripts that are split by topics and contain code examples with explanations.
β’ 18k stars
β’ Explanations next to the code make it easy to learn
6. Practical Python Programming
β’ Designed for people that have some programming experience but donβt know Python yet
β’ 11k stars
β’ Course has itβs own easy to use site
7. Python Programming Exercises
β’ 100+ Python challenging programming exercises
β’ 29k stars
β’ Questions, hints, solutions. Simple and effective way to practice.
π Keep Learning and Building!
1. Python Project Based Learning
β’ Curated list of project based tutorials
β’ 284k stars
β’ Webapps, web scraping, bots, data science, ML, and more.
2. All algorithms implemented in Python
β’ Boatloads of algorithms implemented in Python
β’ 225k stars
β’ Algorithms neatly categorized
3. 30 Days of Python
β’ Step-by-step guide to learn the Python
β’ 74k stars
β’ Logical learning sequence, great explanations and helpful getting started guide
4. 100-Days-Of-ML-Code
β’ A great resource for learning ML
β’ 52k stars
β’ High quality infographic explanations
5. Playground and Cheatsheet for Learning Python
β’ Collection of Python scripts that are split by topics and contain code examples with explanations.
β’ 18k stars
β’ Explanations next to the code make it easy to learn
6. Practical Python Programming
β’ Designed for people that have some programming experience but donβt know Python yet
β’ 11k stars
β’ Course has itβs own easy to use site
7. Python Programming Exercises
β’ 100+ Python challenging programming exercises
β’ 29k stars
β’ Questions, hints, solutions. Simple and effective way to practice.
π Keep Learning and Building!
π4
Python doesn't store variables the way you probably think it does. π§
When you write:
Python doesn't really put
π You can see this more clearly:
Both
The values are equal, but they are two different list objects. π¦
And this idea isn't limited to numbers and lists. In Python, almost everything you work with is an object - strings, functions, classes, dictionaries, and even modules.
Start with this mental model:
Names point to objects. Objects have a type, value, and identity.
That's the Python object model in its simplest form. β
Want more Python concepts explained simplyβ
ππΎFollow the channel so you don't miss them.
When you write:
x = 10
Python doesn't really put
10 inside a box called x. 10 is an object, and x is simply a name that refers to that object. π You can see this more clearly:
x = 10
y = x
print(x is y)
True
Both
x and y are pointing to the same object. This is also why is and == are different:a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True
print(a is b) # False
The values are equal, but they are two different list objects. π¦
And this idea isn't limited to numbers and lists. In Python, almost everything you work with is an object - strings, functions, classes, dictionaries, and even modules.
Start with this mental model:
Names point to objects. Objects have a type, value, and identity.
That's the Python object model in its simplest form. β
Want more Python concepts explained simplyβ
ππΎFollow the channel so you don't miss them.
β€3
Python Programming Roadmap
Stage 1: Python Basics (Syntax, Variables, Data Types)
Stage 2: Control Flow (if/else, loops)
Stage 3: Functions & Modules
Stage 4: Data Structures (Lists, Tuples, Sets, Dicts)
Stage 5: File Handling (Read/Write, CSV, JSON)
Stage 6: Error Handling (try/except, custom exceptions)
Stage 7: Object-Oriented Programming (Classes, Inheritance)
Stage 8: Standard Libraries (os, datetime, math)
Stage 9: Virtual Environments & pip package management
Stage 10: Working with APIs (Requests, JSON data)
Stage 11: Web Development Basics (Flask/Django)
Stage 12: Databases (SQLite, PostgreSQL, SQLAlchemy ORM)
Stage 13: Testing (unittest, pytest frameworks)
Stage 14: Version Control with Git & GitHub
Stage 15: Package Development (setup.py, publishing on PyPI)
Stage 16: Data Analysis (Pandas, NumPy libraries)
Stage 17: Data Visualization (Matplotlib, Seaborn)
Stage 18: Web Scraping (BeautifulSoup, Selenium)
Stage 19: Automation & Scripting projects
Stage 20: Advanced Topics (AsyncIO, Type Hints, Design Patterns)
π Tip: Master one stage before moving to the next. Build mini-projects to solidify your learning.
Stage 1: Python Basics (Syntax, Variables, Data Types)
Stage 2: Control Flow (if/else, loops)
Stage 3: Functions & Modules
Stage 4: Data Structures (Lists, Tuples, Sets, Dicts)
Stage 5: File Handling (Read/Write, CSV, JSON)
Stage 6: Error Handling (try/except, custom exceptions)
Stage 7: Object-Oriented Programming (Classes, Inheritance)
Stage 8: Standard Libraries (os, datetime, math)
Stage 9: Virtual Environments & pip package management
Stage 10: Working with APIs (Requests, JSON data)
Stage 11: Web Development Basics (Flask/Django)
Stage 12: Databases (SQLite, PostgreSQL, SQLAlchemy ORM)
Stage 13: Testing (unittest, pytest frameworks)
Stage 14: Version Control with Git & GitHub
Stage 15: Package Development (setup.py, publishing on PyPI)
Stage 16: Data Analysis (Pandas, NumPy libraries)
Stage 17: Data Visualization (Matplotlib, Seaborn)
Stage 18: Web Scraping (BeautifulSoup, Selenium)
Stage 19: Automation & Scripting projects
Stage 20: Advanced Topics (AsyncIO, Type Hints, Design Patterns)
π Tip: Master one stage before moving to the next. Build mini-projects to solidify your learning.