Python – Lists – Indexing

Previous
Next

Accessing Elements:

            We can access elements of List in 2 ways such as Indexing & Slicing.

Indexing:

  • Python index starts from 0 to size-1.
  • We can access only one element through indexing.
  • Python supports negative indexing also
  • Some of the examples as follow.

Code:

l = [10,20,30,40,50]
print("List is :", l)
print("Length :", len(l))
print("l[2] :", l[2])
print("l[-2] :", l[-2])
print("l[len(l)-2] :", l[len(l)-2])
print("l[-(len(l)-3)] :", l[-(len(l)-3)])

Output:

List is : [10, 20, 30, 40, 50]
Length : 5
l[2] : 30
l[-2] : 40
l[len(l)-2] : 40
l[-(len(l)-3)] : 40
Previous
Next

Add Comment

Courses Enquiry Form