Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
📦 Unit 2 – Strings, Lists, Tuples और Dictionaries  ·  Lesson 23

Tuples: Operations और Methods

Tuple क्या है?

Tuple round brackets ( ) में लिखे items का ordered, immutable sequence है। यह बनने के बाद "lock" हुई list जैसा है — ऐसे data के लिए perfect जो नहीं बदलना चाहिए, जैसे हफ़्ते के दिन, जन्म तिथि या आपके school की GPS location।

days →Mon0-5Tue1-4Wed2-3Thu3-2Fri4-1
Tuple में list जैसे indexes हैं, पर items बदले नहीं जा सकते

Tuples बनाना

Python
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))
20 40 (20, 30) 4 <class 'tuple'> <class 'tuple'> <class 'int'> (1, 2, 3) <class 'tuple'>

Tuples immutable हैं

Python
t = (1, 2)
t[0] = 9
TypeError: 'tuple' object does not support item assignment

आप append, insert या remove भी नहीं कर सकते। del t से पूरा tuple delete कर सकते हैं, या + से नया tuple बना सकते हैं। Tuple में list जैसा mutable item हो तो वह अंदर की list अब भी बदल सकती है:

Python
t = ([1, 2], 3)
t[0].append(9)       # the list inside can still change
print(t)
([1, 2, 9], 3)

Operations, methods और functions

Python
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]))
(1, 2, 3, 4) ('ab', 'ab', 'ab') True 2 4 5 9 1 27 [1, 3, 7, 7, 9] ('h', 'e', 'l', 'l', 'o') (1, 2)
FeatureExample
Indexing / slicingt[0], t[-1], t[1:3], t[::-1]
Concatenation / repetitiont1 + t2, t * 3
Membershipx in t
Methodst.count(x), t.index(x)
Functionslen, 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") देता है।

Python
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)
4 5 6 Ishan 12 88.5 20 10

Nested tuples

Python
students = (('Riya', 91), ('Aman', 85), ('Zoya', 78))
print(students[1][0], students[0][1])
for name, m in students:
    print(f'{name:<6}{m}')
Aman 91 Riya 91 Aman 85 Zoya 78

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
Data बदलेगा या नहीं, इस आधार पर चुनें

Solved programs

1. Numbers tuple में input करके statistics

Python
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))
How many numbers? 3 Number: 14 Number: 9 Number: 25 Tuple: (14, 9, 25) Max: 25 | Min: 9 | Mean: 16.0

2. Tuple return करने वाला function

Python
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)
(78.5, 31.400000000000002) <class 'tuple'> Area = 78.5 | Circumference = 31.400000000000002

लंबा 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 आसान।
✅ Practice

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

अक्सर पूछे जाने वाले प्रश्न (FAQ)

एक element वाला tuple कैसे बनाएँ?
Element के बाद comma लगाएँ: t = (5,)। बिना comma के (5) सिर्फ़ integer 5 है।
List की जगह tuple क्यों use करें?
Tuples बदल नहीं सकते, इसलिए हफ़्ते के दिन या coordinates जैसा fixed data सुरक्षित रखते हैं, थोड़े तेज़ हैं और dictionary keys के रूप में use हो सकते हैं।
Tuple के कौन से methods हैं?
सिर्फ़ दो: count(x) और index(x)। len(), max(), min(), sum() और sorted() जैसे functions भी tuples पर चलते हैं।
← Class 11 Computer Science
🔗

Share this topic with a friend

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

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

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

अगला topicDictionaries: Keys, Values और Methods आपकी प्रगति: 23 / 29 (79%) · Lesson 24 / 29 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें