Important Notice:

Negative Indexing

Negative Indexing

8 views 1 min read
Negative Indexing :-

Python में Negative Indexing का अर्थ है List के elements को अंत (end) से access करना। इसमें List के last element का index -1 होता है और उसके बाद के elements के indexes -2, -3 और इसी प्रकार आगे बढ़ते हैं।

Negative indexing का उपयोग List के elements को right side से access करने के लिए किया जाता है।

English

Negative Indexing means accessing elements of a List from the end (right side). In negative indexing, the last element has the index -1, the second-last element has -2, and so on.

Example:-

numbers = [10, 20, 30, 40, 50]

print(numbers[-1])

print(numbers[-3])

print(numbers[-5])

Output:-

50
30
10

Because:-

यहाँ List के elements और उनके positive तथा negative indexes हैं:

Element : 10 20 30 40 50

Positive: 0 1 2 3 4

Negative: -5 -4 -3 -2 -1

इसलिए:

numbers[-1] → 50

numbers[-2] → 40

numbers[-3] → 30

numbers[-4] → 20

numbers[-5] → 10

Important Point :-

Negative indexing में last element का index हमेशा -1 होता है।

यदि negative index List की सीमा से बाहर हो, तो Python IndexError देता है।

numbers = [10, 20, 30]

print(numbers[-4])

यहाँ -4 index मौजूद नहीं है, इसलिए:

IndexError: list index out of range

Related Notes