LIST IN PYTHON

SAIROSHNI



List

A list is an ordered data structure with elements enclosed within square brackets and separated by commas.


Program:

Mylist=['Pythonbook', 'Javabook']

Print(Mylist)

 


Output:


['Pythonbook', 'Javabook']



List Traversal:

  • List traversal is the sequential access to list elements.Index numbers, or List index, are used to access the list's elements
  • There are 2 types of indexing:
  1. Positive indexing
  2. Negative indexing

Positive Indexing:

      To get to the element from the beginning (from left to right), positive indexing is used.

  Index numbers begin with zero.


List index number

Mylist[0]

Mylist[1]

Mylist[2]

Mylist[3]

Mylist[4]

Element

Bike 

Car 

Bus 

bicycle

tricycle


Program:


Mylist=['bike', 'car', 'bus','bicycle',’tricycle’]
Print(Mylist [0])
Print(Mylist [2]) 


Output:

 

bike
  bus

Negative Indexing:

To get to the element from the end (right to left), negative indexing is used.-1 alludes first last thing, - 2 alludes second last thing, - 3 alludes third keep going, etc.


List index number

Mylist[-1]

Mylist[-2]

Mylist[-3]

Mylist[-4]

Mylist[-5]

Element

Python

Java 

C++ 

Html 

Css 

Program:

 

Mylist=['Python','Java','C++','Html',’Css’]

  Print (Mylist [-1])

 Print (Mylist [-2])

 Print (Mylist [-3])

  Print (Mylist [-4])

 Print (Mylist [-5])



Output:


Css
Html
C++
Java
Python







More topic in Python

Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send