Python Tuples
In this page:
tuple_name = (item1, item2, item3)
tuple_name[index]
Tuple क्या है?
Tuple list जैसा ही एक ordered collection है, पर एक बार बन जाने के बाद उसके elements को reassign, add या remove नहीं किया जा सकता — यह immutability tuples को dictionary keys के रूप में इस्तेमाल करने लायक बनाती है और readers को बताती है कि यह data बदलना नहीं चाहिए, जैसे coordinates (x, y)।
उदाहरण: What is a Tuple?
point = (3, 4)
print(point)
Tuple Items को Access करना
Tuples बिल्कुल वही square-bracket indexing और slicing support करते हैं जो lists करती हैं, इसलिए my_tuple[0] और my_tuple[1:3] बिल्कुल वैसे ही काम करते हैं — असली फ़र्क बस इतना है कि बाद में उनके through assign नहीं किया जा सकता।
उदाहरण: Accessing Tuple Items
point = (3, 4, 5)
print(point[0]) # index like a list
print(point[1:3]) # slice like a list, returns a tuple
Tuple Unpacking
Unpacking से आप x, y = point लिखकर किसी tuple की values को एक ही line में named variables में निकाल सकते हैं, यही वजह है कि कई values लौटाने वाले functions (जैसे divmod()) उन्हें tuple के रूप में लौटाते हैं जिन्हें आप तुरंत destructure कर सकते हैं।
उदाहरण: Tuple Unpacking
point = (3, 4)
x, y = point # unpacks the tuple's values into x and y
print(x, y)
Tuple Operations
आप किसी tuple को in place modify नहीं कर सकते, पर + दो tuples को जोड़कर एक बिल्कुल नया tuple बना देता है और * किसी tuple के elements को repeat करता है, दोनों ही originals को छुए बिना नए tuple objects बनाते हैं।
उदाहरण: Tuple Operations
a = (1, 2)
b = (3, 4)
print(a + b) # concatenates into a new tuple
print(a * 2) # repeats a's elements into a new tuple
Tuple Methods
चूँकि tuples न बढ़ सकते हैं न सिकुड़ सकते हैं, उन्हें सिर्फ count(value) (कोई value कितनी बार आती है) और index(value) (उसकी पहली position) की ज़रूरत होती है — support करने के लिए कोई append या remove नहीं है।
उदाहरण: Tuple Methods
t = (1, 2, 2, 3)
print(t.count(2)) # how many times 2 appears
print(t.index(3)) # position of the first 3
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: