Nauč se Python > Materiály > Snake Workshop for PyLadies > Introduction to Python > Lists

Lists – Řešení [2]

>>> lottery
[59, 42, 30, 19, 12, 3, 199]

>>> lottery[3]
19

Index 3 means the fourth element.

>>> lottery[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

Element with index 7 does not exist - it results in error.

>>> loterie[1000]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

Same for index 1000.

>>> loterie[-1]
199

Index -1 means the last element.

>>> loterie[-2]
3

Index -2 means the one before the last element.

>>> loterie[-6]
42

Index -6 means the sixth element from the end.

>>> loterie[-100]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

Such element does not exist, we get error.


Toto je stránka lekce z kurzu, který probíhá nebo proběhl naživo s instruktorem.