Important Notice:

Indexing in Tuple

Indexing in Tuple

8 views 2 min read
Indexing in Tuple :-
Python में Indexing in Tuple का अर्थ है Tuple के elements को उनके index number (position number) की सहायता से access करना। प्रत्येक element का एक unique index होता है, जिसका उपयोग उस element को प्राप्त करने के लिए किया जाता है।

Tuple में indexing 0 से शुरू होती है, इसलिए पहला element index 0 पर होता है।

English

Indexing in Tuple means accessing the elements of a tuple using their index numbers (position numbers). Each element has an index that can be used to access that element.

Python uses zero-based indexing, so the first element has index 0.

Example:-
 
numbers = (10, 20, 30, 40, 50)

print(numbers[0])
print(numbers[2])
print(numbers[4])

Output:-

10
30
50

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

Each element can be accessed using its corresponding index number.

Positive Indexing :-

Tuple में left से right की ओर indexing को Positive Indexing कहा जाता है।

colors = ("Red", "Green", "Blue", "Yellow")

print(colors[0])
print(colors[3])

Output:-

Red
Yellow

यहाँ indexing 0 से शुरू होकर 3 तक जाती है।

Negative Indexing :-

Tuple में right से left की ओर indexing को Negative Indexing कहा जाता है। इसमें last element का index -1 होता है।

numbers = (10, 20, 30, 40, 50)

print(numbers[-1])
print(numbers[-3])

Output:-

50
30

Index Table:

Element : 10 | 20 | 30 | 40 | 50
Positive:     0 | 1   |  2  |  3  | 4
Negative:  -5 | -4  | -3  | -2  | -1

Indexing with Variable :-

Index को किसी variable में store करके भी Tuple element access किया जा सकता है।

numbers = (10, 20, 30, 40)

index = 2

print(numbers[index])

Output:-

30

यहाँ index = 2 है, इसलिए numbers[2] अर्थात 30 access होगा।

Index Out of Range :-

यदि हम Tuple में मौजूद index से बाहर का index access करते हैं, तो Python IndexError देता है।

numbers = (10, 20, 30)

print(numbers[5])

यहाँ 5 index मौजूद नहीं है।

Output/Error:

IndexError: tuple index out of range

Related Notes