Accessing Tuple Elements :-
Python में Accessing Tuple Elements का अर्थ है Tuple में मौजूद किसी particular element (value) को प्राप्त करना या program में use करना। Tuple के elements को मुख्य रूप से उनके Index Number की सहायता से access किया जाता है।
Tuple में indexing 0 से शुरू होती है। इसलिए पहला element index 0, दूसरा element index 1 और इसी प्रकार आगे होता है।
English
Accessing Tuple Elements means retrieving or using a particular element (value) from a tuple. Tuple elements are mainly accessed using their index numbers.
Python uses zero-based indexing, so the first element has index 0, the second element has index 1, and so on.
Example:-
numbers = (10, 20, 30, 40, 50)
print(numbers[0])
print(numbers[2])
Output:-
10
30
Because:-
यहाँ Tuple के elements और उनके indexes हैं:
Element : 10 20 30 40 50
Index : 0 1 2 3 4
इसलिए:
numbers[0] → 10
numbers[1] → 20
numbers[2] → 30
numbers[3] → 40
numbers[4] → 50
English
Here, each tuple element is accessed using its corresponding index number.
Accessing Tuple Elements Using a Variable :-
किसी index को variable में store करके भी element access किया जा सकता है।
numbers = (10, 20, 30, 40, 50)
index = 3
print(numbers[index])
Output:-
40
यहाँ index की value 3 है, इसलिए numbers[3] अर्थात 40 access होगा।
Accessing Multiple Tuple Elements :-
Tuple के multiple elements को slicing की सहायता से access किया जा सकता है।
numbers = (10, 20, 30, 40, 50)
print(numbers[1:4])
Output:-
(20, 30, 40)
यहाँ index 1 से 4 तक elements लिए गए हैं, लेकिन stop index 4 include नहीं होता।
English
Multiple tuple elements can be accessed using slicing. The start index is included, but the stop index is excluded.
Accessing Tuple Elements Using Negative Indexing :-
Tuple के elements को end से access करने के लिए negative indexing का उपयोग किया जा सकता है।
numbers = (10, 20, 30, 40, 50)
print(numbers[-1])
print(numbers[-3])
Output:-
50
30
Because:-
Negative indexing में:
numbers[-1] → 50
numbers[-2] → 40
numbers[-3] → 30
numbers[-4] → 20
numbers[-5] → 10
Important Point :-
यदि Tuple में मौजूद index से बाहर का index access किया जाता है, तो Python IndexError देता है।
numbers = (10, 20, 30)
print(numbers[5])
यहाँ index 5 मौजूद नहीं है, इसलिए:
IndexError: tuple index out of range
In Short :-
Accessing Tuple Elements → Index number की सहायता से Tuple के elements को प्राप्त करना।
Positive Indexing → 0 से शुरू होती है।
Negative Indexing → -1 से शुरू होती है।
Slicing → एक से अधिक elements को access करने के लिए उपयोग होती है।