Theory
Duplicate Entry का सिरदर्द
आपके Semester 1 BCA104 C labs में, अगर आपको expense transactions दर्शाते numbers की एक श्रृंखला मिली और आपको उन items की unique categories पता करनी थीं जिन पर आपने पैसे ख़र्च किए, आपको थकाऊ nested loops लिखने पड़ते। आपको हर item देखना, एक accumulation array scan करना, और हाथ से जाँचना पड़ता कि वह item पहले से गिना गया या नहीं। Python इस समय लेने वाली lookup logic को पूरी तरह क्यों छोड़ देता है, एक ऐसा collection देते हुए जो अपने-आप duplicate values reject करता है और raw records को uniqueness के लिए तुरंत filter करता है?
Theory
Entry Token Drop-Box
एक college campus event में एक physical drop-box की कल्पना कीजिए जहाँ students numbered tokens डालते हैं। अगर पाँच students 'Token 10' marked identical tokens डालते हैं, जादुई box उन्हें एक अकेले physical token में merge करता है। इसके अलावा, जब आप box हिलाते हैं, tokens बिना fixed positions के बेतरतीब खनकते हैं। अब एक 'पहले' या 'आख़िरी' token का कोई concept नहीं है। आप जल्दी पूछ सकते हैं कि क्या एक ख़ास token अंदर मौजूद है, पर आप कभी 'index 0 पर item' नहीं माँग सकते क्योंकि सामग्री unordered निलंबन की एक निरंतर अवस्था में है।
Theory
Set Mechanics औपचारिक रूप से
Python में एक set curly braces {} के भीतर घिरा unique elements का एक unordered, mutable collection है। क्योंकि sets unordered हैं, वे एक insertion record या ऐतिहासिक sequence बनाए नहीं रखते। नतीजतन, sets में index lookup (set[0]) या sequence slicing के लिए support पूरी तरह नहीं है। Sets duplicate values नहीं रख सकते; declaration या runtime mutation के दौरान append किया कोई भी duplicate तुरंत निगल और condense किया जाता है।
At a glance
Table 1: elements manipulation और mathematical updates के लिए ज़रूरी Python set methods।
| Method Tool | Memory पर की गई Action | Error / Exception Behavior |
|---|---|---|
| items.add(val) | set में एक नई unique primitive value डालता है। | value पहले से मौजूद होने पर चुपचाप अनदेखा। |
| items.remove(val) | set से एक ख़ास value सीधे delete करता है। | value ग़ायब होने पर एक तुरंत KeyError फेंकता है। |
| items.discard(val) | set से एक ख़ास value सुरक्षित रूप से delete करता है। | value ग़ायब होने पर शून्य errors के साथ चुपचाप विफल। |
| items.pop() | set से एक मनमाना element हटाता और return करता है। | एक empty set पर call करने पर एक KeyError फेंकता है। |
| set1.union(set2) | दोनों sets से items को एक बिल्कुल नए set में जोड़ता है। | original source sets को पूरी तरह unmodified छोड़ता है। |
Theory
Worked Example: Unique Expense Tags का Audit
आइए देखें कि हमारा PocketMoney tracking system set collections का लाभ कैसे उठाता है। हम uniqueness निकालने के लिए दोहराई item categories रखती एक list को एक set constructor में feed करेंगे, duplicate addition नियम आज़माएँगे, और सख़्त remove tool की safe discard tool से तुलना करेंगे।
Practical
PocketMoney Unique Category Auditor
# Step 1: Converting a list with duplicate entries into a unique set
raw_categories = ["food", "travel", "food", "canteen", "travel"]
unique_tags = set(raw_categories)
# Step 2: Attempting to add an existing element vs a new one
unique_tags.add("food")
unique_tags.add("stationery")
# Step 3: Removing an item using the safe discard method
unique_tags.discard("travel")
# Step 4: Removing an item safely even if it doesn't exist
unique_tags.discard("entertainment")
print("Audited Unique Set:", unique_tags)
print("Is 'food' present?:", "food" in unique_tags)This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
Set Content State को ट्रेस करें
ऊपर के Python tracking script का मन में विश्लेषण कीजिए। print होने पर unique_tags के अंदर कौन से elements बचेंगे? क्या order हमारे code statement sequence से मेल खाएगा?
Show the answer
script output करेगी (element order बदल सकता है):
Audited Unique Set: {'canteen', 'food', 'stationery'}
Is 'food' present?: True
क्यों? raw_categories convert करना {'food', 'travel', 'canteen'} देता है। 'food' जोड़ना कुछ नहीं करता क्योंकि यह एक duplicate है। 'stationery' जोड़ना इसे {'food', 'travel', 'canteen', 'stationery'} तक फैलाता है। discard('travel') call करना इसे साफ़-सुथरे delete करता है। discard('entertainment') call करना कुछ नहीं करता और कोई errors नहीं फेंकता। क्योंकि sets unordered हैं, output layout order आपकी screen पर मनमाने ढंग से दिख सकता है।
Quiz
क्या होता है अगर एक programmer एक blank empty set collection initialize करने की उम्मीद में line: ledger = {} लिखता है?
- Python शून्य length वाला एक valid empty set बनाता है।
- यह एक set के बजाय एक empty dictionary collection initialize करता है।
- script एक SyntaxError के साथ crash होती है क्योंकि empty braces प्रतिबंधित हैं।
- यह एक immutable frozen tuple wrapper बनाता है।
Show the answer
यह एक set के बजाय एक empty dictionary collection initialize करता है।
क्योंकि curly braces sets और dictionaries दोनों द्वारा साझा हैं, और dictionaries ऐतिहासिक रूप से पहले आईं, Python literal expression {} को एक empty dictionary object पर default करता है। एक असली empty set बनाने के लिए, आपको constructor function स्पष्ट रूप से call करना होगा: ledger = set()।
Quiz
इस script snippet पर विचार कीजिए: codes = {"A", "B", "C"} print(codes[0])
- यह "A" print करता है।
- यह machine memory पर निर्भर एक random character print करता है।
- यह एक TypeError फेंकता है: 'set' object is not subscriptable।
- यह एक IndexOutOfBoundsException फेंकता है।
Show the answer
यह एक TypeError फेंकता है: 'set' object is not subscriptable।
Sets बिना positional markers या numeric keys के पूरी तरह unordered collections हैं। चूँकि elements के कोई ख़ास slots नहीं हैं, एक index lookup करने के लिए bracket notation इस्तेमाल करना पूरी तरह प्रतिबंधित है और एक तुरंत runtime TypeError trigger करता है।
Watch out
Classic जाल: Remove Crash बनाम Discard Safety
university laboratory examinations में सबसे बार-बार marks-गँवाने वाली ग़लती बिना verification के एक set से parameters scrub करने के लिए मनमाने ढंग से .remove() इस्तेमाल करना है। अगर target value dataset से अनुपस्थित हो, .remove() एक तुरंत runtime KeyError crash पैदा करता है। हमेशा .discard() इस्तेमाल कीजिए जब आप items को सुरक्षित रूप से मिटाना चाहते हैं बिना अपने program को crash किए अगर item पहले से delete था।
Theory
Sets को Semester 3 से जोड़ना
Set tracking advanced database cross-referencing layers पर सीधे map होता है। Semester 3 (BCA303) में, जब एक SQLite engine से निकाले दो अलग relational tables या log datasets की तुलना करते हैं, आप अपने query data fields को Python sets में convert कर सकते हैं और एक ही step में sync anomalies या overlapping profiles अलग करने के लिए .union(), .intersection(), या differences चला सकते हैं।
Summary
Key takeaways
- Sets mutable, पूरी तरह unordered collections हैं जिनमें सख़्ती से unique items होते हैं।
- Sets entry initialization या modification पर अपने-आप duplicate values discard करते हैं।
- index coordinates के ज़रिए positional lookup block है क्योंकि sets में संरचित order नहीं है।
- remove method elements drop करती है पर item ग़ायब होने पर एक KeyError फेंकती है।
- discard tool elements को सुरक्षित रूप से drop करता है बिना कोई runtime exceptions पैदा किए।
- Memory Hook: Braces mixed sets रखते हैं, blanks dicts बनाते हैं, sets indexing discard करते हैं!