indexing syntax in list

 indexing syntax in list :


syntax : list_name[index_number]

this is used for to get a specified item from the list, you need to things that is name of list and the index number you want to get. 


list_name[index_number]

list name refers to name of the list

index number refers to index number of that item


in this above diagram 

  • the first item item1 is indexed  with 0
  • the second item item2 indexed with 1
  • the third item item3 is indexed with 2

like this

Thumb Rule for Indexing 


the index number is always one less to the item position in the list.

my_list=['a', 'b', 'c']

print(my_list[0])

Output :

a

Index number is higher

my_list=['a', 'b', 'c']

print(my_list[10])

Output :

<index error : list index out of range on line 2>


negative index :

negative index numbers starts from last to first.

my_list=['a', 'b', 'c']

print(my_list[-1])

Output :

c


  • You have to must try in your python compiler use idle avoid online compilers to Errors

Post a Comment

0 Comments