Lesson 06 — Foundation

TUPLES & SETS:
FIXED AUR UNIQUE DATA

Kabhi kabhi data change nahi hona chahiye — jaise coordinates ya RGB values. Tuple woh collection hai jo ek baar banne ke baad fix rehta hai. Aur Set? Woh sirf unique values rakhta hai, duplicates ko kaat deta hai.

⏱ 28 min BeginnerPrerequisite: Lists

Masala Daani samjho: dabba = variable (container ka naam)

LIST — [] square brackets
"haldi"102.53+4jTrue
order fix • badal sakte ho
TUPLE — () gol brackets
"haldi"102.53+4jTrue
order fix • fix hai, badal nahi sakte
SET — {} curly, bina key
"haldi"102.53+4jTrue
order nahi • duplicate gayab
DICT — {key: value}
"masala": "haldi""daam": 10"vajan": 2.5"code": 3+4j"fresh": True
key se nikalo • badal sakte ho

Ek dabba (variable) ke andar 4 dibbi — aur har dibbi me wahi 5 masale: string, int, float, complex, bool. Aage har lesson apni dibbi khol kar dikhayega.

WHY: tuples aur sets kyun zaruri hain

Tuples unmutable collections hain jab data change nahi hona chahiye (coordinates, RGB values, function returns). Sets unique values store karte hain aur mathematical set operations support karte hain.

FIXED

Tuple ek baar banne ke baad change nahi hota.

UNIQUE

Set sirf unique values rakhta hai.

ORDERED

Tuple ordered hai, Set unordered.

WHAT: tuples aur sets basic syntax

python▶ Run
# Tuples
colors = ("red", "green", "blue")
print(colors[0]) # red
print(len(colors)) # 3

# Tuple unpacking
x, y, z = colors
print(x) # red

# Sets
unique_numbers = {1, 2, 3, 2, 1}
print(unique_numbers) # {1, 2, 3}

# Set operations
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # union: {1, 2, 3, 4, 5}
print(a & b) # intersection: {3}

WHEN: tuples aur sets kab use karein

Tuples tab use karo jab data fixed hona chahiye (coordinates, database rows). Sets tab jab duplicates remove karne ho ya set operations (union, intersection) karo.

Try it: tuples & sets playgroundCoordinates aur unique students ke saath practice karo
Run Python dabayein

Useful tuple & set operations

Quick check

Ek tuple banao jisme 3 cities ho: Lucknow, Delhi, Mumbai. Answer mein tuple use karo.

Tuple parentheses mein hota hai aur values comma-separated hote hain.

Unpack karna: khol ke baant lo

Tuple ke items seedha alag variables me daal do — Swiggy order ka unpack, ya do variables ki adla-badli ek line me. Extra items * se pakdo.

python▶ Run
order = ("SWIGGY-4821", 250, "paid")
oid, amount, status = order
print(oid, amount, status)

a, b = 10, 20
a, b = b, a
print(a, b)  # 20 10 (swap!)

first, *rest = [1, 2, 3, 4]
print(first, rest)  # 1 [2, 3, 4]

Frozenset aur methods table

Set ke andar duplicate apne aap gayab — coupon codes ya cities ki unique list ke liye best. frozenset wahi set hai jo badal hi nahi sakta (dict ki key ban sakta hai).

python▶ Run
cities = ["Delhi", "Lucknow", "Delhi"]
print(set(cities))  # {Delhi, Lucknow} order fix nahi

s = {1, 2}
s.add(3)
s.discard(2)
print(s)  # {1, 3}

print((5,).count(5))  # tuple: count/index chalte hain
MethodKya karta haiExample
add(x)Set me dalnas.add(3)
remove(x)Hatana (na mile to error)s.remove(3)
discard(x)Hatana (safe)s.discard(9)
pop()Ek nikalnas.pop()
union / |Dono ka joda | b
intersection / &Common walea & b
difference / -Farak walea - b

Quick check

Ek element wala tuple likho jisme 5 ho.

Brackets ke andar comma lagana mat bhoolo — wahi tuple banata hai.

🎓
Guido-style insight: Tuple isliye hai kyunki kuch cheezein badalni hi nahi chahiye — function ke 2 return values, dictionary ki key, fixed config. Badlav rokna bhi ek feature hai.
python▶ Run
# Swap — interview favourite
a = 5
b = 9
a, b = b, a
print(a, b)  # 9 5

# Dict key me tuple chalega, list nahi
loc = {(28.6, 77.2): "Delhi"}
print(loc[(28.6, 77.2)])

MNC tip: "List vs tuple?" — mutability + hashability bolo, phir swap trick dikhao.

Loop aur join karna

Tuple par loop list jaisa hi — order fix hai. Do tuple + se judte hain, do baar chahiye to * se.

python▶ Run
t1 = ("Delhi", "Lucknow")
for city in t1:
    print(city)

t2 = (1, 2)
t3 = (3, 4)
print(t2 + t3)  # (1, 2, 3, 4)

Set par loop aur join

Set par bhi loop chalta hai — bas order fix nahi hota. | se union, & se common wale.

python▶ Run
s = {"Delhi", "Lucknow"}
for city in s:
    print(city)

a = {1, 2}
b = {2, 3}
print(a | b)  # union
print(a & b)  # intersection

Common mistakes

Tuples & Sets clear?

Ab key-value pairs ke saath Dictionaries dekhte hain.

Popular courses: Data Science Course in Lucknow · AI Course in Lucknow · Python Course in Lucknow · Power BI Course · Data Analytics Course