Indexing, slicing, and matrices

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'

 

..................Content has been hidden....................

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