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:
- Positive indexing
- 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.
Program:
Mylist=['bike', 'car', 'bus','bicycle',’tricycle’]
Print(Mylist [0])
Print(Mylist [2])
Print(Mylist [0])
Print(Mylist [2])
Output:
bike
bus
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.
Program:
Mylist=['Python','Java','C++','Html',’Css’]
Print (Mylist [-1])
Print (Mylist [-2])
Print (Mylist [-3])
Print (Mylist [-4])
Print (Mylist [-5])
Mylist=['Python','Java','C++','Html',’Css’]
Print (Mylist [-1])
Print (Mylist [-2])
Print (Mylist [-3])
Print (Mylist [-4])
Print (Mylist [-5])
Print (Mylist [-1])
Print (Mylist [-2])
Print (Mylist [-3])
Print (Mylist [-4])
Print (Mylist [-5])
Output:
Css
Html
C++
Java
Python
Css
Html
C++
Java
Python
Html
C++
Java
Python
More topic in Python