पायथन प्रोग्रामिंग मास्टरक्लास: शुरुआती से एडवांस्ड डेवलपर तक
पायथन आज दुनिया की सबसे लोकप्रिय और बहुमुखी (Versatile) प्रोग्रामिंग भाषा है। वेब डेवलपमेंट (Django/FastAPI) से लेकर डेटा साइंस, मशीन लर्निंग और ऑटोमेशन तक — पायथन की आसान पठनीयता (Readability) और समृद्ध इकोसिस्टम इसे अद्वितीय बनाते हैं। आइए इसके सभी आवश्यक सिद्धांतों को वर्किंग कोड के साथ समझें।
विषय सूची (Table of Contents):
1. CPython, Bytecode और PVM कैसे काम करता है?
पायथन एक विशुद्ध रूप से इंटरप्रिटेड भाषा नहीं है; यह एक हाइब्रिड (Compiled + Interpreted) प्रणाली है:
- जब आप
script.pyचलाते हैं, तो CPython कंपाइलर पहले इसे इंटरमीडिएट Bytecode (.pycफाइल) में कंपाइल करता है। - इसके बाद Python Virtual Machine (PVM) इस बाइटकोड को एक-एक निर्देश करके मशीन कोड में ट्रांसलेट करके चलाता है।
GIL (Global Interpreter Lock): CPython का एक अंतर्निहित लॉक जो एक समय में केवल एक थ्रेड को पायथन बाइटकोड निष्पादित करने की अनुमति देता है। सीपीयू-बाउंड मल्टी-थ्रेडिंग के लिए multiprocessing मॉड्यूल का उपयोग किया जाता है।
2. Core Data Structures: List, Tuple, Dict, Set
| डेटा स्ट्रक्चर | सिंटैक्स | म्यूटेबल (Mutable)? | डुप्लीकेट्स? | सर्च टाइम कॉम्प्लेक्सिटी |
|---|---|---|---|---|
| List | [1, 2, 3] | हाँ (बदला जा सकता है) | अनुमति है | O(N) |
| Tuple | (1, 2, 3) | नहीं (Immutable) | अनुमति है | O(N) (मेमोरी में हल्का) |
| Dictionary | {'key': 'val'} | हाँ | कीज़ यूनिक होनी चाहिए | O(1) औसत (Hash Table) |
| Set | {1, 2, 3} | हाँ | डुप्लीकेट्स स्वतः हट जाते हैं | O(1) औसत (Hash Table) |
3. Object-Oriented Programming (OOP) in Python
पायथन में क्लास, कंस्ट्रक्टर (__init__), इनहेरिटेंस और पॉलीमॉर्फिज्म का संपूर्ण कोड उदाहरण:
class Employee:
# क्लास वेरिएबल
company_name = "CubeX Technologies"
# कंस्ट्रक्टर
def __init__(self, name: str, salary: float):
self.name = name # पब्लिक एट्रिब्यूट
self.__salary = salary # प्राइवेट एट्रिब्यूट (इनकैप्सुलेशन)
# गटर मेथड
def get_salary(self):
return self.__salary
# मेथड
def get_details(self):
return f"{self.name} works at {self.company_name}"
# इनहेरिटेंस (वारिस)
class Developer(Employee):
def __init__(self, name: str, salary: float, programming_language: str):
super().__init__(name, salary)
self.language = programming_language
# मेथड ओवरराइडिंग (पॉलीमॉर्फिज्म)
def get_details(self):
return f"Developer {self.name} codes in {self.language}"
# ऑब्जेक्ट बनाना
dev = Developer("Rohit", 75000, "Python")
print(dev.get_details()) # Developer Rohit codes in Python
print(f"Salary: {dev.get_salary()}")
4. Decorators और Generators
Decorator (@decorator)
किसी फंक्शन के मूल कोड को बदले बिना उसमें अतिरिक्त कार्यक्षमता (जैसे Execution Time नापना, Logging, Authentication) जोड़ने का तरीका:
import time
def timer(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
print(f"{func.__name__} took {time.time() - start:.4f}s")
return result
return wrapper
@timer
def heavy_calc():
return sum(range(1000000))
heavy_calc()
Generator (yield)
पूरा डेटा एक साथ रैम में लोड करने के बजाय एक-एक आइटम ऑन-डिमांड जनरेट करता है (मेमोरी-कुशल):
def countdown(n):
while n > 0:
yield n
n -= 1
# 10 लाख नंबर भी 0 RAM में प्रोसेस होंगे!
for val in countdown(3):
print(val) # 3, 2, 1
5. Comprehensions और File Handling
# List Comprehension
squares = [x**2 for x in range(10) if x % 2 == 0]
# [0, 4, 16, 36, 64]
# Context Manager के साथ सुरक्षित File I/O
with open("user_data.txt", "w", encoding="utf-8") as f:
f.write("सुरक्षित फाइल राइटिंग जो स्वतः क्लोज हो जाती है।")
# Exception Handling
try:
num = 10 / 0
except ZeroDivisionError as e:
print(f"एरर पकड़ा गया: {e}")
finally:
print("यह ब्लॉक हमेशा चलेगा (Clean-up)।")