โก๏ธ Python Sets: Stop Nesting Loops for Comparisons
Nested loops to find common items between lists are a performance killer. As your data grows, checking
Python Sets use hash tables to turn these comparisons into lightning-fast math operations.
๐ฏ Stop "searching" through lists to find overlaps or differences. Convert your data to
Nested loops to find common items between lists are a performance killer. As your data grows, checking
if item in list inside another loop slows down your code exponentially. Python Sets use hash tables to turn these comparisons into lightning-fast math operations.
# Two lists with 1 million items
list_a = list(range(1_000_000))
list_b = list(range(500_000, 1_500_000))
# โ THE SLOW WAY: Nested lookup (O(n^2))
# This could take minutes on large lists
# common = [x for x in list_a if x in list_b]
# โ THE PRO WAY: Set Math (O(n))
# This happens almost instantly
common = set(list_a) & set(list_b) # Intersection
diff = set(list_a) - set(list_b) # Items in A but not B
๐ฏ Stop "searching" through lists to find overlaps or differences. Convert your data to
set() and use math symbols (&, -, ^) to handle large-scale comparisons in milliseconds.โค5
Forwarded from Free Programming Books
Annotated Algorithms in Python.pdf
2.6 MB
๐ Annotated Algorithms in Python
โ๏ธ Author: Massimo Di Pierro
๐ Year: 2021
๐ Pages: 376
๐ง This open book is assembled from lectures given by the author over a period of 10 years at the School of Computing of DePaul University. The lectures cover multiple classes, including Analysis and Design of Algorithms, Scientific Computing, Monte Carlo Simulations, and Parallel Algorithms. These lectures teach the core knowledge required by any scientist interested in numerical algorithms and by students interested in computational finance.
#Algorithms
โ๏ธ Author: Massimo Di Pierro
๐ Year: 2021
๐ Pages: 376
๐ง This open book is assembled from lectures given by the author over a period of 10 years at the School of Computing of DePaul University. The lectures cover multiple classes, including Analysis and Design of Algorithms, Scientific Computing, Monte Carlo Simulations, and Parallel Algorithms. These lectures teach the core knowledge required by any scientist interested in numerical algorithms and by students interested in computational finance.
#Algorithms
โค4
โAsynchronous Programming in Python
Asynchronous programming allows applications to handle multiple tasks simultaneously without blocking. This is especially useful for I/O-bound operations, such as web requests, where waiting can lead to inefficiencies.
โKey Concepts
โข Event Loop: Manages and dispatches events or tasks.
โข Coroutines: Functions defined with
โข Tasks: Wrappers for coroutines that run concurrently.
โBenefits
1. Improved Performance: Handles more requests in less time.
2. Better Resource Utilization: Non-blocking I/O optimizes system resource use.
3. Responsive Applications: Keeps user interfaces responsive during background processing.
โGetting Started with
The
โExplanation
โข
โข
โข
โReal-World Application: Web Scraping
Using
Asynchronous programming allows applications to handle multiple tasks simultaneously without blocking. This is especially useful for I/O-bound operations, such as web requests, where waiting can lead to inefficiencies.
โKey Concepts
โข Event Loop: Manages and dispatches events or tasks.
โข Coroutines: Functions defined with
async def that can pause execution.โข Tasks: Wrappers for coroutines that run concurrently.
โBenefits
1. Improved Performance: Handles more requests in less time.
2. Better Resource Utilization: Non-blocking I/O optimizes system resource use.
3. Responsive Applications: Keeps user interfaces responsive during background processing.
โGetting Started with
asyncioThe
asyncio library provides the tools for asynchronous programming. Hereโs a simple example simulating data fetching from multiple URLs:import asyncio
import random
async def fetch_data(url):
print(f"Fetching data from {url}...")
await asyncio.sleep(random.uniform(1, 3)) # Simulate network delay
print(f"Data fetched from {url}")
return f"Data from {url}"
async def main():
urls = ["http://example.com", "http://example.org", "http://example.net"]
tasks = [fetch_data(url) for url in urls]
results = await asyncio.gather(*tasks)
print("All data fetched:", results)
# Run the main function
asyncio.run(main())
โExplanation
โข
fetch_data(url): An asynchronous function simulating data fetching.โข
asyncio.sleep(): A non-blocking sleep that allows other tasks to run.โข
asyncio.gather(): Runs multiple coroutines concurrently.โReal-World Application: Web Scraping
Using
aiohttp, you can perform asynchronous HTTP requests efficiently. Hereโs an example:import aiohttp
import asyncio
async def fetch(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
async def scrape(urls):
tasks = [fetch(url) for url in urls]
return await asyncio.gather(*tasks)
urls = ["http://example.com", "http://example.org", "http://example.net"]
# Run the scraping function
results = asyncio.run(scrape(urls))
print("Scraped data:", results)
โค4
Forwarded from Programming Quiz Channel
What is the output of this code?
x = [1, 2, 3]
y = x y.append(4) print(len(x))
x = [1, 2, 3]
y = x y.append(4) print(len(x))
Anonymous Quiz
23%
3
41%
4
25%
Error
11%
Undefined
โค4
๐ How to Learn Python Fast (Even If You've Never Coded Before)
Python is everywhere. Web dev, data science, automation, AIโฆ
But where should YOU start if you're a beginner?
Donโt worry. Hereโs a 6-step roadmap to master Python the smart way (no fluff, just action)๐
๐น ๐ฆ๐๐ฒ๐ฝ ๐ญ: Learn the Basics (Donโt Skip This!)
โ Variables, data types (int, float, string, bool)
โ Loops (for, while), conditionals (if/else)
โ Functions and user input
Start with:
Python.org Docs
YouTube: Programming with Mosh / CodeWithHarry
Platforms: W3Schools.com / LearnDevs.com / FreeCodeCamp.org
Spend a week here.
Practice > Theory.
๐น ๐ฆ๐๐ฒ๐ฝ ๐ฎ: Automate Boring Stuff (Itโs Fun + Useful!)
โ Rename files in bulk
โ Auto-fill forms
โ Web scraping with BeautifulSoup or Selenium
Read: โAutomate the Boring Stuff with Pythonโ
Itโs beginner-friendly and practical!
๐น ๐ฆ๐๐ฒ๐ฝ ๐ฏ: Build Mini Projects (Your Confidence Booster)
โ Calculator app
โ Dice roll simulator
โ Password generator
โ Number guessing game
These small projects teach logic, problem-solving, and syntax in action.
๐น ๐ฆ๐๐ฒ๐ฝ ๐ฐ: Dive Into Libraries (Pythonโs Superpower)
โ Pandas and NumPy - for data
โ Matplotlib - for visualizations
โ Requests - for APIs
โ Tkinter - for GUI apps
โ Flask - for web apps
Libraries are what make Python powerful. Learn one at a time with a mini project.
๐น ๐ฆ๐๐ฒ๐ฝ ๐ฑ: Use Git + GitHub (Be a Real Dev)
โ Track your code with Git
โ Upload projects to GitHub
โ Write clear README files
โ Contribute to open source repos
Your GitHub profile = Your online CV. Keep it active!
๐น ๐ฆ๐๐ฒ๐ฝ ๐ฒ: Build a Capstone Project (Level-Up!)
โ A weather dashboard (API + Flask)
โ A personal expense tracker
โ A web scraper that sends email alerts
โ A basic portfolio website in Python + Flask
Python is everywhere. Web dev, data science, automation, AIโฆ
But where should YOU start if you're a beginner?
Donโt worry. Hereโs a 6-step roadmap to master Python the smart way (no fluff, just action)๐
๐น ๐ฆ๐๐ฒ๐ฝ ๐ญ: Learn the Basics (Donโt Skip This!)
โ Variables, data types (int, float, string, bool)
โ Loops (for, while), conditionals (if/else)
โ Functions and user input
Start with:
Python.org Docs
YouTube: Programming with Mosh / CodeWithHarry
Platforms: W3Schools.com / LearnDevs.com / FreeCodeCamp.org
Spend a week here.
Practice > Theory.
๐น ๐ฆ๐๐ฒ๐ฝ ๐ฎ: Automate Boring Stuff (Itโs Fun + Useful!)
โ Rename files in bulk
โ Auto-fill forms
โ Web scraping with BeautifulSoup or Selenium
Read: โAutomate the Boring Stuff with Pythonโ
Itโs beginner-friendly and practical!
๐น ๐ฆ๐๐ฒ๐ฝ ๐ฏ: Build Mini Projects (Your Confidence Booster)
โ Calculator app
โ Dice roll simulator
โ Password generator
โ Number guessing game
These small projects teach logic, problem-solving, and syntax in action.
๐น ๐ฆ๐๐ฒ๐ฝ ๐ฐ: Dive Into Libraries (Pythonโs Superpower)
โ Pandas and NumPy - for data
โ Matplotlib - for visualizations
โ Requests - for APIs
โ Tkinter - for GUI apps
โ Flask - for web apps
Libraries are what make Python powerful. Learn one at a time with a mini project.
๐น ๐ฆ๐๐ฒ๐ฝ ๐ฑ: Use Git + GitHub (Be a Real Dev)
โ Track your code with Git
โ Upload projects to GitHub
โ Write clear README files
โ Contribute to open source repos
Your GitHub profile = Your online CV. Keep it active!
๐น ๐ฆ๐๐ฒ๐ฝ ๐ฒ: Build a Capstone Project (Level-Up!)
โ A weather dashboard (API + Flask)
โ A personal expense tracker
โ A web scraper that sends email alerts
โ A basic portfolio website in Python + Flask
โค5