🟢 Beginner · Lesson 13
Python Tuples और Sets
Tuples और Sets
Lists के अलावा Python में दो और collections हैं: tuple (क्रमबद्ध पर बदली नहीं जा सकती) और set (अक्रमित, केवल unique items)। हर एक का अपना काम है।
Tuple — Ordered और Fixed
Tuple round brackets ( ) use करता है। यह immutable है — एक बार बन जाने पर items बदले नहीं जा सकते। ऐसे data के लिए जो बदलना नहीं चाहिए, जैसे coordinates या fixed config।
Python – tuple.py
point = (10, 20) print(point[0]) # list की तरह access print(len(point)) # point[0] = 5 -> ERROR: tuples बदल नहीं सकते
10
2
Set — Unique और Unordered
Set curly braces { } use करता है और अपने आप duplicates हटाता है। इसमें कोई order नहीं, इसलिए indexing नहीं।
Python – set.py
nums = {1, 2, 2, 3, 3, 3}
print(nums) # duplicates गायब
nums.add(4)
print(nums){1, 2, 3}
{1, 2, 3, 4}
Program 1: Tuple Basics
Python – tbasics.py
colors = ("red", "green", "blue")
for c in colors:
print(c)
print("Total:", len(colors))red
green
blue
Total: 3
Program 2: कई Values लौटाना
Function से एक से ज़्यादा value लौटाने के लिए tuples बढ़िया हैं।
Python – minmax.py
def min_max(nums):
return min(nums), max(nums) # tuple लौटाता है
low, high = min_max([5, 2, 9, 1])
print("Low:", low, "High:", high)Low: 1 High: 9
Function दो values tuple के रूप में लौटाता है; हम उन्हें low और high में unpack करते हैं।
Program 3: Set से Duplicates हटाएं
Python – dedupe.py
marks = [85, 90, 85, 78, 90, 100]
unique = set(marks)
print("Unique marks:", unique)
print("How many unique:", len(unique))Unique marks: {100, 85, 90, 78}
How many unique: 4
List को set में बदलते ही repeated values हट जाती हैं।
Program 4: Set Operations
Python – setops.py
maths = {"Aman", "Riya", "Karan"}
science = {"Riya", "Karan", "Sara"}
print("Both subjects :", maths & science) # intersection
print("Any subject :", maths | science) # union
print("Only maths :", maths - science) # differenceBoth subjects : {'Riya', 'Karan'}
Any subject : {'Aman', 'Riya', 'Karan', 'Sara'}
Only maths : {'Aman'}
&= दोनों में (intersection)।|= किसी में भी (union)।-= पहले में पर दूसरे में नहीं (difference)।
List बनाम Tuple बनाम Set
| Feature | List [ ] | Tuple ( ) | Set { } |
|---|---|---|---|
| Ordered | हाँ | हाँ | नहीं |
| Changeable | हाँ | नहीं | हाँ (add/remove) |
| Duplicates | allowed | allowed | नहीं |
| Indexing | हाँ | हाँ | नहीं |
सामान्य गलतियाँ
- tuple item बदलने की कोशिश (यह immutable है)।
- set से order या indexing की उम्मीद करना।
- 1-item tuple को
(5)लिखना — यह(5,)comma के साथ होना चाहिए।
Practice Tasks
- सप्ताह के दिन tuple में store करके हर एक print करें।
- Duplicates वाली list लेकर सिर्फ unique values print करें।
- Sets से दो lists के common elements ढूंढें।
- एक function लिखें जो sum और average दोनों tuple में लौटाए।
सारांश
- Tuple: ordered, immutable, duplicates allowed —
( )। - Set: unordered, unique items, no indexing —
{ }। - Set operations:
&intersection,|union,-difference।
💻 लाइव कोड एडिटर
इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.