Tuples and Sets in Python
Python Tuples और Sets
Tuples and Sets
Besides lists, Python has two more collections: tuple (ordered but cannot be changed) and set (unordered, only unique items). Each has its own job.
Tuple — Ordered & Fixed
A tuple uses round brackets ( ). It is immutable — once created, items cannot be changed. Use it for data that should not change, like coordinates or fixed config.
point = (10, 20) print(point[0]) # access like a list print(len(point)) # point[0] = 5 -> ERROR: tuples cannot change
Set — Unique & Unordered
A set uses curly braces { } and automatically removes duplicates. It has no order, so no indexing.
nums = {1, 2, 2, 3, 3, 3}
print(nums) # duplicates gone
nums.add(4)
print(nums)Program 1: Tuple Basics
colors = ("red", "green", "blue")
for c in colors:
print(c)
print("Total:", len(colors))Program 2: Returning Multiple Values
Tuples are great for returning more than one value from a function.
def min_max(nums):
return min(nums), max(nums) # returns a tuple
low, high = min_max([5, 2, 9, 1])
print("Low:", low, "High:", high)The function returns two values as a tuple; we unpack them into low and high.
Program 3: Remove Duplicates with Set
marks = [85, 90, 85, 78, 90, 100]
unique = set(marks)
print("Unique marks:", unique)
print("How many unique:", len(unique))Converting a list to a set instantly removes repeated values.
Program 4: Set Operations
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) # difference&= in both (intersection).|= in either (union).-= in first but not second (difference).
List vs Tuple vs Set
| Feature | List [ ] | Tuple ( ) | Set { } |
|---|---|---|---|
| Ordered | Yes | Yes | No |
| Changeable | Yes | No | Yes (add/remove) |
| Duplicates | Allowed | Allowed | Not allowed |
| Indexing | Yes | Yes | No |
Common Mistakes
- Trying to change a tuple item (it is immutable).
- Expecting a set to keep order or allow indexing.
- Writing a 1-item tuple as
(5)— it must be(5,)with a comma.
Practice Tasks
- Store the days of the week in a tuple and print each.
- Take a list with duplicates and print only unique values.
- Find common elements between two lists using sets.
- Write a function that returns both sum and average as a tuple.
Summary
- Tuple: ordered, immutable, allows duplicates — use
( ). - Set: unordered, unique items, no indexing — use
{ }. - Set operations:
&intersection,|union,-difference.
Tuples और Sets
Lists के अलावा Python में दो और collections हैं: tuple (क्रमबद्ध पर बदली नहीं जा सकती) और set (अक्रमित, केवल unique items)। हर एक का अपना काम है।
Tuple — Ordered और Fixed
Tuple round brackets ( ) use करता है। यह immutable है — एक बार बन जाने पर items बदले नहीं जा सकते। ऐसे data के लिए जो बदलना नहीं चाहिए, जैसे coordinates या fixed config।
point = (10, 20) print(point[0]) # list की तरह access print(len(point)) # point[0] = 5 -> ERROR: tuples बदल नहीं सकते
Set — Unique और Unordered
Set curly braces { } use करता है और अपने आप duplicates हटाता है। इसमें कोई order नहीं, इसलिए indexing नहीं।
nums = {1, 2, 2, 3, 3, 3}
print(nums) # duplicates गायब
nums.add(4)
print(nums)Program 1: Tuple Basics
colors = ("red", "green", "blue")
for c in colors:
print(c)
print("Total:", len(colors))Program 2: कई Values लौटाना
Function से एक से ज़्यादा value लौटाने के लिए tuples बढ़िया हैं।
def min_max(nums):
return min(nums), max(nums) # tuple लौटाता है
low, high = min_max([5, 2, 9, 1])
print("Low:", low, "High:", high)Function दो values tuple के रूप में लौटाता है; हम उन्हें low और high में unpack करते हैं।
Program 3: Set से Duplicates हटाएं
marks = [85, 90, 85, 78, 90, 100]
unique = set(marks)
print("Unique marks:", unique)
print("How many unique:", len(unique))List को set में बदलते ही repeated values हट जाती हैं।
Program 4: Set Operations
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) # difference&= दोनों में (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।