A method is the same thing as a function, except it is “called on” a value.List values have an index() method that can be passed a value, and if that value exists in the list, the index of the value is returned. If the value isn’t
in the list, then Python produces a ValueError error. Enter the following into the interactive shell.
>>> teams = ['chelsea','RealMadrib','Barcelona','Juventus','Inter-Milano','Bayern-Munich']
>>> teams.index('Barcelona')
2
>>> teams .index('Manchester')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'Manchester' is not in list
>>>
Top comments (0)