Negative index in python by R4R Team

What is index ?

index is the actual position of the character in the sequence, generally index in programming language is start with the 0.

Example -
l=[5,2,3,8,9]

here number '5' is at index '0'
and number '8' is at index '3'

positive index is valid in all programming languages but negative index is also supported by the python programming language.

Negative index -

l=[1,2,3,4,5]

so here number '5' is at index '-1'
and number '3' is at index '-3'

program -

l=[1,2,3,4,5]
#positive index
print("On positive index")
print(l[0])
print(l[1])
print(l[2])
print(l[3])
print(l[4])

#negative index
print("On negative index")
print(l[-1])
print(l[-2])
print(l[-3])
print(l[-4])
print(l[-5])


output -

On positive index
1
2
3
4
5
On negative index
5
4
3
2
1
[5, 4, 3, 2, 1]



Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!