Accessing values in lists

We can access the values from a list by using index values. We will specify the index number in [ and ]. Index starts from 0. Refer to the given example:

#!/usr/bin/python3
cities = ['Mumbai', 'Bangalore', 'Chennai', 'Pune']
numbers = [1, 2, 3, 4, 5, 6, 7 ]
print (cities[0])
print (numbers[1:5])


Output:
Mumbai
[2, 3, 4, 5]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset