Page 93 - TP_V5.1_C8_fb
P. 93
ACCESSING LIST ITEMS
We use an index number to access the list items just like strings. Use the index operator [ ] to
access the elements of a list.
Program 15: To access the different elements of a list.
Program15.py
File Edit Format Run Options Window Help
List1=[1,2,3,11,12,13,'Computer',40, 'Science'] Output
print (List1[2])
3
print (List1[6])
Computer
print (List1[-2])) 40
LIST FUNCTIONS
Let us study some of the functions we can use with lists.
The append() Function
The append() function inserts the object passed to it at the end of the list. Syntax of using append()
function is:
list_name.append(item)
Program 16: To add an element to a list using append() function.
Program16.py
File Edit Format Run Options Window Help
a=[1,2,3,4,5]
a.append(6) Output
print (a) [1, 2, 3, 4, 5, 6]
The extend( ) Function
Using the append( ) function, we can only add one element at a time. For adding more than one
element, we use the extend( ) function. Syntax of using extend() function is:
list_name.extend(iterable value)
Program 17: To add elements to an existing list using extend() function.
Program17.py
File Edit Format Run Options Window Help
a=[1,2,3,4,5]
a.extend([6,7,8,9])
print (a)
Functions, String and List in Python 91

