Give an example to access values in lists.
Answers
in pyton
Accessing Values in Lists
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 −
Live Demo
#!/usr/bin/python
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]
When the above code is executed, it produces the following result −
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
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 −
Live Demo
#!/usr/bin/python
list = ['physics', 'chemistry', 1997, 2000];
print "Value available at index 2 : "
print list[2]
list[2] = 2001;
print "New value available at index 2 : "
print list[2]
hope it is helpfull
Accessing values in lists in python:
To access values in lists, we always use the square brackets for slicing sideways with the index or indices to get value available at that index. For example –
list1 = ['ecology', 'statistics', 1992, 2000];
list2 = [11, 22, 33, 44, 55, 66, 77];
Print "list1 [0]: “list1 [0]
Print "list2 [1:5]: “list2 [1:5]