🟢 Beginner  ·  Lesson 13

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.

Python – tuple.py
point = (10, 20)
print(point[0])      # access like a list
print(len(point))
# point[0] = 5  ->  ERROR: tuples cannot change
10 2

Set — Unique & Unordered

A set uses curly braces { } and automatically removes duplicates. It has no order, so no indexing.

Python – set.py
nums = {1, 2, 2, 3, 3, 3}
print(nums)          # duplicates gone
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: Returning Multiple Values

Tuples are great for returning more than one value from a function.

Python – minmax.py
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)
Low: 1 High: 9

The function returns two values as a tuple; we unpack them into low and high.

Program 3: Remove Duplicates with Set

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

Converting a list to a set instantly removes 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)   # difference
Both subjects : {'Riya', 'Karan'} Any subject : {'Aman', 'Riya', 'Karan', 'Sara'} Only maths : {'Aman'}
  • & = in both (intersection).
  • | = in either (union).
  • - = in first but not second (difference).

List vs Tuple vs Set

FeatureList [ ]Tuple ( )Set { }
OrderedYesYesNo
ChangeableYesNoYes (add/remove)
DuplicatesAllowedAllowedNot allowed
IndexingYesYesNo

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

  1. Store the days of the week in a tuple and print each.
  2. Take a list with duplicates and print only unique values.
  3. Find common elements between two lists using sets.
  4. 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।

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)   # difference
Both subjects : {'Riya', 'Karan'} Any subject : {'Aman', 'Riya', 'Karan', 'Sara'} Only maths : {'Aman'}
  • & = दोनों में (intersection)।
  • | = किसी में भी (union)।
  • - = पहले में पर दूसरे में नहीं (difference)।

List बनाम Tuple बनाम Set

FeatureList [ ]Tuple ( )Set { }
Orderedहाँहाँनहीं
Changeableहाँनहींहाँ (add/remove)
Duplicatesallowedallowedनहीं
Indexingहाँहाँनहीं

सामान्य गलतियाँ

  • tuple item बदलने की कोशिश (यह immutable है)।
  • set से order या indexing की उम्मीद करना।
  • 1-item tuple को (5) लिखना — यह (5,) comma के साथ होना चाहिए।

Practice Tasks

  1. सप्ताह के दिन tuple में store करके हर एक print करें।
  2. Duplicates वाली list लेकर सिर्फ unique values print करें।
  3. Sets से दो lists के common elements ढूंढें।
  4. एक function लिखें जो sum और average दोनों tuple में लौटाए।

सारांश

  • Tuple: ordered, immutable, duplicates allowed — ( )
  • Set: unordered, unique items, no indexing — { }
  • Set operations: & intersection, | union, - difference।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n

💻 Live Code Editor

Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें. Agar load na ho to naye tab mein kholें.