Lab-3 Python
Lab-3 Python
The list is a most versatile datatype available in Python which can be written
as a list of comma-separated values (items) between square brackets.
Important thing about a list is that items in a list need not be of the same
type.
To access values in lists, use the square brackets for slicing along with the
index or indices to obtain value available at that index. For example −
#!/usr/bin/python
list2 = [1, 2, 3, 4, 5, 6, 7 ]
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
Page 1 of 3
Updating Lists
You can update single or multiple elements of lists by giving the slice on the
left-hand side of the assignment operator, and you can add to elements in a
list with the append() method. For example −
#!/usr/bin/python
print list[2]
list[2] = 2001
print list[2]
#!/usr/bin/python
print list1
del list1[2]
Page 2 of 3
print list1
Page 3 of 3