Tuples: Operations और Methods
Tuple क्या है?
Tuple round brackets ( ) में लिखे items का ordered, immutable sequence है। यह बनने के बाद "lock" हुई list जैसा है — ऐसे data के लिए perfect जो नहीं बदलना चाहिए, जैसे हफ़्ते के दिन, जन्म तिथि या आपके school की GPS location।
Tuples बनाना
t = (10, 20, 30, 40) print(t[1], t[-1], t[1:3], len(t), type(t)) one = (5,) # comma makes it a tuple not_tuple = (5) # just the number 5 in brackets print(type(one), type(not_tuple)) p = 1, 2, 3 # brackets are optional (packing) print(p, type(p))
Tuples immutable हैं
t = (1, 2) t[0] = 9
आप append, insert या remove भी नहीं कर सकते। del t से पूरा tuple delete कर सकते हैं, या + से नया tuple बना सकते हैं। Tuple में list जैसा mutable item हो तो वह अंदर की list अब भी बदल सकती है:
t = ([1, 2], 3) t[0].append(9) # the list inside can still change print(t)
Operations, methods और functions
t = (10, 20, 30, 40)
print((1, 2) + (3, 4), ('ab',) * 3, 20 in t)
x = (3, 7, 1, 7, 9)
print(x.count(7), x.index(9))
print(len(x), max(x), min(x), sum(x))
print(sorted(x)) # returns a LIST
print(tuple('hello'), tuple([1, 2]))| Feature | Example |
|---|---|
| Indexing / slicing | t[0], t[-1], t[1:3], t[::-1] |
| Concatenation / repetition | t1 + t2, t * 3 |
| Membership | x in t |
| Methods | t.count(x), t.index(x) |
| Functions | len, max, min, sum, sorted (list देता है), tuple() |
Tuple assignment (packing और unpacking)
Packing कई values को एक tuple में रखता है (p = 1, 2, 3)। Unpacking उन्हें अलग variables में निकालता है। Variables की संख्या items के बराबर होनी चाहिए, नहीं तो Python ValueError ("too many values to unpack") देता है।
a, b, c = (4, 5, 6) # unpacking print(a, b, c) name, roll, marks = 'Ishan', 12, 88.5 print(name, roll, marks) x, y = 10, 20 x, y = y, x # swap = tuple packing + unpacking print(x, y)
Nested tuples
students = (('Riya', 91), ('Aman', 85), ('Zoya', 78))
print(students[1][0], students[0][1])
for name, m in students:
print(f'{name:<6}{m}')Tuple vs list
List [ ]
- Mutable – जोड़, हटा, बदल सकते हैं
- कई methods (append, sort …)
- बदलने वाले data के लिए: cart items, marks entry
Tuple ( )
- Immutable – बनने के बाद fixed
- सिर्फ़ count() और index()
- तेज़, सुरक्षित; dictionary key बन सकता है
- Fixed data के लिए: months, (x, y) points
Solved programs
1. Numbers tuple में input करके statistics
n = int(input('How many numbers? '))
t = ()
for i in range(n):
t = t + (int(input('Number: ')),) # build a new tuple each time
print('Tuple:', t)
print('Max:', max(t), '| Min:', min(t), '| Mean:', sum(t) / len(t))2. Tuple return करने वाला function
def circle(r):
area = 3.14 * r * r
circumference = 2 * 3.14 * r
return area, circumference # a tuple
result = circle(5)
print(result, type(result))
a, c = circle(5)
print('Area =', a, '| Circumference =', c)लंबा decimal float approximation से आता है; 31.4 दिखाने के लिए round(c, 2) use करें।
सारांश और practice
- Tuple = ordered, immutable,
( )में; एक item के लिए comma ज़रूरी। - Indexing, slicing, +, *, in, len, max, min, sum चलते हैं।
- Methods: count(), index()। sorted() list देता है।
- Packing/unpacking से swap और कई returns आसान।
Output बताइए: t = (2, 4, 6, 8); print(t[1:3] * 2, t.index(6), 8 in t)। Tuple का दूसरा सबसे बड़ा element और even elements की गिनती निकालने का program लिखें।
Answer: (4, 6, 4, 6) 2 True
