Page 132 - TP_Play_V2.2_Class8
P. 132
Program 3: To change the elements of a list
Program3.py
File Edit Format Run Options Window Help
list1=[13, 25, 41, 63, 82]
list1[1]=50
Output
list1[3]=45
print(list1[:]) [13, 50, 41, 45, 82]
TRAVERSING A LIST
Traversing means accessing or visiting the elements of a list. Two ways to traverse a list are positive
indexing and negative indexing.
Positive Indexing
In positive indexing, the index of elements of a list starts from from left to right which means if a list
contains 10 elements then its index (it is always an integer number) is from 0 to 9.
Factbot
If the user tries to access an element from a list beyond its defined range, then it will give an IndexError.
To access the list elements, the indexing operator or subscript operator [ ] is used.
Program 4: To find the elements in the list using the index value
Program4.py
File Edit Format Run Options Window Help
list1=[13, 25, 41, 63, 82]
Output
print(list1[0]) 13
print(list1[3]) 63
print(list1[4]) 82
Factbot
If a user tries to access a list element by using floating-point indexing, it will result in
IndexError.
130 Plus (Ver. 4.0)-VIII

