Python Advanced Concepts - Complete Detailed Guide
text1. List Comprehensions
List comprehensions Python ka ek concise syntax hai lists create karne ke liye loops ke saath.
squares = [x**2 for x in range(6)] print(squares) #text
2. Lambda Functions (Anonymous Functions)
Lambda ek chhoti anonymous function hai jo ek expression ko evaluate karta hai.
add = lambda x, y: x + y print(add(3, 5)) # 8text
3. Map, Filter, Reduce
map()
- ek function ko sequence ke har element par apply karta hai.filter()
- sequence me se condition true hone wale elements ko filter karta hai.reduce()
- sequence ko accumulate karne ke liye function apply karta hai (functools module se).
from functools import reduce nums = squared = list(map(lambda x: x**2, nums)) evens = list(filter(lambda x: x % 2 == 0, nums)) sum_all = reduce(lambda x, y: x + y, nums) print(squared) # print(evens) # print(sum_all) # 15text
4. Generators
Generators memory efficient tareeke se iterators banate hain, jisme data lazily generate hota hai.
def count_up_to(max): count = 1 while count <= max: yield count count += 1 counter = count_up_to(5) for num in counter: print(num)text
5. Decorators
Decorator ek function ko modify karne ka way hai kisi aur function ke behaviour ko bina usse modify kiye.
def decorator_func(original_func): def wrapper(): print("Before call") original_func() print("After call") return wrapper @decorator_func def say_hello(): print("Hello!") say_hello()text
6. Context Managers
with
keyword ke saath use hota hai jo resource management easy banata hai (jaise file handling).
with open("file.txt", "r") as file: data = file.read() print(data)text
7. Iterators and Iterables
Python me jo objects iteration support karte hain unhe iterables kehte hain. Iterator ek object hai jo next element provide karta hai.
my_list = iter_obj = iter(my_list) print(next(iter_obj)) # 1 print(next(iter_obj)) # 2text
8. Closures
Closure wo function hota hai jo apne environment ke variables ko yaad rakhta hai.
def outer(): message = "Hello" def inner(): print(message) return inner func = outer() func() # Hellotext
9. Memory Management
Python me garbage collection, reference counting hota hai jo unused objects ko memory se hata deta hai. Mutable aur immutable objects me fark hota hai jo performance aur behaviour ko effect karta hai.
10. Summary
Ye advanced Python concepts aapko powerful aur efficient programs likhne me madad karte hain. Inka use data processing, functional programming, resource management, aur modular coding me hota hai. Inhe seekhna aapki programming skills ko next level par le jata hai.