Advance Python
Unit - 1
Q) 1. Python Data Types
Python में data types बहुत ही important concept है क्योंकि िोई भी variable जो हम बनाते हैं, उसिे अंदर िोई ना िोई
type िा data होता है — जैसे number, text, list वगैरह। Python एि dynamically typed language है, मतलब हमें variable
बनाते वक्त type specify नहीं िरना पड़ता, Python खुद समझ जाती है कि किस type िा data है। Python में िई प्रिार
िे data types होते हैं, जैसे — integer, float, string, boolean, list, tuple, set, dictionary और NoneType.
सबसे पहले बात िरते हैं integer (int) िी — इसमें हम whole numbers store िरते हैं, जैसे 5, 100, -50. किर आता है float,
जजसमें decimal वाले numbers आते हैं जैसे 5.5, 3.14 — ये बहुत िाम आता है जब हमें पैसों या percentage िा
calculation िरना हो। String (str) एि बहुत ही common data type है जो characters और text िो store िरता है, जैसे
किसी िा नाम, address या िोई message। String हमेशा quotes िे अंदर ललखा जाता है — single या double दोनों चल
जाता है।
अब आते हैं boolean (bool) पर, जो लसिफ दो values लेता है: True या False. ये logical conditions और decision making में
िाम आता है — जैसे किसी student ने pass किया या नहीं। उसिे बाद आता है
list, जो एि ordered collection होती है, जजसमें िई items एि साथ store किए जाते हैं, और हम उन्हें update/change भी
िर सिते हैं। List िो square brackets [] में ललखा जाता है।
अगर हमें ऐसा collection चाहहए जो change ना हो, तो हम tuple िा use िरते हैं — ये भी list जैसा होता है लेकिन
immutable होता है यानी इसमें data once set, then fix रहता है। Tuple िो round brackets () में ललखा जाता है।
किर आता है set, जो unordered collection होता है और इसमें duplicate values automatically हट जाती हैं। Set िो curly
braces {} में define किया जाता है।
सबसे मजेदार और powerful data type है dictionary (dict) — ये key-value pairs में data store िरता है, जैसे एि student
िा नाम और उसिी age एि साथ store िरना हो तो dictionary perfect है। इसे भी curly brackets {} में ललखा जाता है,
लेकिन अंदर colon : use होता है key और value िो अलग िरने िे ललए।
NoneType, जो ऐसे variable िो represent िरता है जजसमें अभी िोई value नहीं है। यानी हम future में उसे use िरने
िे ललए define तो िर दे ते हैं, लेकिन value अभी नहीं दे ते — तब उसमें None assign किया जाता है।
Python में इन data types िी knowledge होना बहुत ज़रूरी है क्योंकि इनिा सही use िरने से हमारा code efficient और
readable बनता है। इसललए जब भी Python सीखें या ललखें — data types पर अच्छी पिड़ ज़रूरी है।
Q) 2. How Memory is Managed in Python
Python में memory management automatic होता है , मतलब programmer िो manually memory allocate या free
िरने िी ज़रूरत नहीं होती। Python internally बहुत smart होता है — वो खुद ही decide िरता है कि किस variable
िो कितनी memory दे नी है और िब उस memory िो free िरना है। ये सब िाम Python िे Memory Manager और
Garbage Collector नाम िे systems िरते हैं। जब भी हम िोई variable बनाते हैं, तो Python उसिे ललए
automatically memory allocate िर दे ता है, और जैसे ही वो variable िा िाम खत्म हो जाता है (मतलब उसिा िोई
reference नहीं बचता), तो Python िा Garbage Collector उसे memory से हटा दे ता है। इससे system िी
performance और efficiency दोनों बनी रहती है।
Python में दो main memory allocation types होती हैं — Static Memory Allocation और Dynamic Memory
Allocation।
Static allocation िा मतलब होता है कि िुछ memory Python internally predefined तरीिे से reserve िरिे रखता
है, जैसे built-in functions, global variables आहद िे ललए।
Dynamic memory allocation में runtime िे दौरान memory allocate िी जाती है — जैसे जब हम list, dictionary या
िोई object बनाते हैं।
Python हर variable िो एि object िी तरह treat िरता है और हर object िे पीछे एि reference count होता है।
जजतने ज्यादा references, उतना ज्यादा memory reserved।
लेकिन जैसे ही reference count zero हो जाता है, Python समझ जाता है कि अब इस memory िी ज़रूरत नहीं है और
Garbage Collector उसे साि िर दे ता है।
Example िे तौर पर, मान लो तूने एि variable बनाया x = [1, 2, 3] — अब Python memory में इस list िे ललए जगह
बना दे ता है और x उस memory िो point िरता है। अगर तू y = x िर दे , तो एि और reference बन गया। लेकिन जैसे
ही तू दोनों िो delete िर दे ता है, Python automatically उस memory िो free िर दे ता है — यही होता है Reference
Counting और Garbage Collection िा िाम।
1. Static Memory Allocation in Python
Static memory allocation िा मतलब है कि memory पहले से ही reserve िर दी जाती है, जब program run होता है
उससे पहले ही। हालांकि Python एि dynamic language है, लेकिन िुछ चीज़ें जैसे built-in objects, functions, या
global constants िो interpreter compile time पर ही memory assign िर दे ता है। इसिा िायदा ये होता है कि
performance fast होती है और memory access efficient होता है। Example िे ललए, जब Python interpreter चालू
होता है तो िुछ system-level constants और modules िी memory पहले से allocate हो जाती है ताकि बाद में बार-
बार allocation ना िरना पड़े। हालांकि Python में ज़्यादातर memory dynamic होती है, लेकिन interpreter level पर
िुछ चीज़ें static होती हैं ताकि system fast िाम िरे ।
Exam Line: "Static memory allocation में memory program िे run होने से पहले दी जाती है।"
2. Dynamic Memory Allocation in Python
Dynamic memory allocation Python िी सबसे बड़ी ताित है। इसिा मतलब होता है कि memory runtime पर
allocate होती है, यानन program िे चलते वक्त जजतनी ज़रूरत हो, उतनी memory ली जाती है। जब हम िोई list बनाते
हैं, dictionary या custom object create िरते हैं — Python उस वक्त ही memory assign िरता है। इस तरह िा
allocation flexible होता है क्योंकि हमें पहले से नहीं बताना होता कि कितनी memory लगेगी। ये िाम Python िा
internal memory manager िरता है जो memory allocate िरता है और reference counting से track िरता है कि
िौन-िौन उस memory िो use िर रहा है।
Example:
x = [10, 20, 30] # अब Python ने इस list िे ललए memory runtime पर allocate िी
Exam Line: "Dynamic memory allocation में memory run-time पर assign होती है, जजससे flexibility बढ़ती है।"
3. Reference Counting in Python
Python हर object िे साथ एि reference count रखता है — मतलब ये track किया जाता है कि कितनी जगहों से उस
object िो refer किया जा रहा है। जब िोई नया variable उसी object िो point िरता है, तो count बढ़ जाता है, और
जब variable delete होता है या िोई और value assign िी जाती है, तो count घटता है। जैसे ही reference count 0
होता है, Python िो समझ में आता है कि अब ये object किसी िाम िा नहीं रहा, और किर वो इसे delete िरने िे
ललए Garbage Collector िो signal दे ता है।
Example:
x = [1, 2, 3]
y = x # अब दो reference हो गए
del x
del y # अब reference count 0, object delete हो जाएगा
Exam Line: "Reference counting से Python decide िरता है कि object िो िब memory से हटाना है।"
4. Garbage Collection in Python
Garbage collection एि ऐसी process है जो Python में automatically unwanted memory िो साि िरता है। जब
िोई object use में नहीं रहता, यानन उसिा reference count zero हो जाता है, तो Garbage Collector उसे
automatically memory से हटा दे ता है ताकि system िी memory leak ना हो और performance degrade ना हो।
Python िा gc module इस िाम िो manage िरता है और हम चाहें तो इसे manually भी trigger िर सिते हैं। ये
process Java जैसी बड़ी languages से ललया गया है और Python में िािी smart तरीिे से implement किया गया है।
Example:
import gc
[Link]() # manually garbage collection चालू किया
Exam Line: "Garbage collection memory िो clean रखता है और unnecessary objects िो delete िरता है।"
Q) 3. PEP8 in Python — Exam-Ready Long Paragraph
Python में coding िरते वक़्त लसिफ logic ही important नहीं होता, बजकि code िी readability और clean structure भी
बहुत ज़रूरी होता है। इसी िो ध्यान में रखते हुए Python ने एि coding style guide बनाया है जजसे िहते हैं PEP8
(Python Enhancement Proposal 8)। ये एि official guideline है जो बताता है कि Python code िो िैसे ललखना
चाहहए ताकि वो readable, understandable और consistent रहे। मान लो 100 लोग एि ही project पर िाम िर रहे
हैं, अगर सब अपने-अपने हहसाब से code ललखेंगे तो िोई किसी िा code समझ नहीं पाएगा। लेकिन अगर सब PEP8
follow िरें , तो code universal standard में ललखा जाएगा और आसानी से समझा जा सिता है। PEP8 बताता है कि
variable naming िैसा होना चाहहए, indentations कितने space िे हों, functions और classes िे बीच कितना space
रखा जाए, line िी length कितनी होनी चाहहए, imports िैसे arrange िरने चाहहए, और comments िैसे ललखने चाहहए।
ये सारे छोटे -छोटे rules लमलिर एि अच्छा और professional code बनाते हैं।
उदाहरण िे ललए, PEP8 िहता है कि indentation हमेशा 4 spaces से होनी चाहहए (tabs से नहीं), variable और
function िे नाम lowercase में होने चाहहए और words िो underscore से जोड़ना चाहहए जैसे student_name, class
िा नाम हमेशा CamelCase में होना चाहहए जैसे StudentDetails, और एि function और दस
ू रे function िे बीच दो
blank lines होने चाहहए। इससे ना लसिफ code सुंदर हदखता है, बजकि समझने में भी time नहीं लगता। Import
statements भी PEP8 में तीन भागों में बााँटे जाते हैं — standard libraries, third-party libraries और किर local
application imports, और सबिो अलग-अलग lines में रखना चाहहए।