January 2019
Beginner
318 pages
8h 23m
English
Tuple indices work the same way as list 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 tuple named cities and perform some index operations:
cities = ('Mumbai', 'Bangalore', 'Chennai', 'Pune')
|
Description |
Expression |
Results |
|
Index starts 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