January 2019
Beginner
318 pages
8h 23m
English
List indices work the same way as string indices. Values can be accessed using index. If you try to read or write an element that does not exist, you get IndexError. If an index has a negative value, it counts backward from the end of the list.
Now, we will create a list named cities and we will see the index operations:
cities = ['Mumbai', 'Bangalore', 'Chennai', 'Pune']
|
Description |
Expression |
Results |
|
Index start at zero |
cities[2] |
'Chennai' |
|
Slicing: getting sections |
cities[1:] |
['Bangalore', 'Chennai', 'Pune'] |
|
Negative: count from the right |
cities[-3] |
'Bangalore' |
Read now
Unlock full access