Nauč se Python > Kurzy > Hadí workshop @CodeWeekEU > Úvod do Pythonu > Seznamy

Seznamy – Řešení [2]

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

>>> loterie[3]
19

Index 3 označuje čtvrtý prvek.

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

Prvek s indexem 100 v seznamu není – nastane chyba.

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

Prvek s indexem 7 v seznamu taky není.

>>> loterie[-1]
199

Index -1 označuje poslední prvek.

>>> loterie[-2]
3

Index -2 označuje předposlední prvek.

>>> loterie[-6]
42

Index -6 označuje šestý prvek od konce.

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

Stý prvek od konce v seznamu není. Nastane chyba.


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