
Mastering Intermediate Python: A Practical Guide
Take your Python skills to the next level with this structured, mobile-friendly tutorial for intermediate learners. Perfect for coders who’ve mastered the basics and are ready to build real-world skills.
1. Lambda Functions
square = lambda x: x * x
print(square(5)) # Output: 25
2. Map, Filter, Reduce
from functools import reduce
nums = [1, 2, 3, 4]
squared = list(map(lambda x: x**2, nums))
even = list(filter(lambda x: x % 2 == 0, nums))
product = reduce(lambda x, y: x * y, nums)
3. OOP: Classes and Inheritance
class Car:
def __init__(self, brand):
self.brand = brand
def drive(self):
print(f"{self.brand} is driving.")
class ElectricCar(Car):
def drive(self):
print(f"{self.brand} is silently driving.")
4. Error Handling
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")
5. File Handling
with open("data.txt", "w") as file:
file.write("Hello, Python!")
6. Modules and Packages
# greetings.py
def hello(name):
return f"Hello, {name}!"
# main.py
import greetings
print(greetings.hello("Alice"))
7. Decorators
def my_decorator(func):
def wrapper():
print("Before function call")
func()
print("After function call")
return wrapper
@my_decorator
def greet():
print("Hello!")
greet()
8. Generators
def countdown(n):
while n > 0:
yield n
n -= 1
for num in countdown(5):
print(num)
9. List and Dict Comprehensions
squares = [x**2 for x in range(10)]
squared_dict = {x: x**2 for x in [1, 2, 3]}
10. Virtual Environments
python -m venv myenv
source myenv/bin/activate # macOS/Linux
myenv\Scripts\activate # Windows
11. Pythonic Tips
# Swapping
x, y = 1, 2
x, y = y, x
# Truthy checks
if []: # False
print("Empty list")
12. Project Ideas
- To-Do CLI App
- Web Scraper with BeautifulSoup
- Weather Dashboard using APIs
- Password Manager
- Email Automation Bot
Conclusion
Congratulations! You now have the tools to build more robust, scalable Python applications. Keep practicing, and explore advanced topics like web development, data science, or automation.
Comments
Post a Comment