Python 3.9.0 | autor: Vlastimil Čevela 2021
formát volání metody index na objektu "seznam":
PozY = Seznam.index (PolX [, *Pozice-Od[, *Pozice-Do]])
Dat-Int Lit-Str/Int
Fce-Int Dat-Str/Int
*Pozice-Od/Do
Lit-Int
Dat-Int
Fce-Int
příklady A - práce s metodou "index"
print(" ----- #1")
List = ["Franta", "Karel", "Marie", "Hana", "Karel"]
PolX = "Karel"
PozY = List.index(PolX)
print(PozY, PolX)
print(" ----- #2")
print(List.index(PolX), PolX)
print(List.index("Marie"))
print(" ----- #3")
try:
PolX = "karel"
print(List.index(PolX))
except ValueError:
print(PolX, "není v seznamu !!!")
print(" ----- #4")
List = [265, -45, 0, 223, 789]
PolX = -45
print(List.index(PolX), PolX)
print(" ----- #5")
print(List.index("marie"))
příklady B -
print(" ----- #1")
List = ["Franta", "Karel", "Marie", "Hana", "Karel"]
print(List)
print(" ----- #2")
for PolX in List:
print(PolX)
print(" ----- #3")
for PolX in List:
print(List.index(PolX), PolX)
příklady C - ...
print(" ----- #1")
List = [456, 237, 1283, 0, 22, 0]
print(List)
print(" ----- #2")
PozY = 0
for PolX in List:
print(PozY, PolX)
PozY += 1
print(" ----- #3")
PozY = 0
for PolX in List:
if PolX == 0:
print(PozY, PolX, "- vybraná položka")
else:
print(PozY, PolX)
PozY += 1
print(" ----- #4")
print(len(List), "= počet položek v seznamu")
PozX = 20
try:
print(List[PozX])
except IndexError:
print("požadovaná pozice", PozX, "je mimo mimo sezunam - !!!")
print(" ----- #5")
PozX = -4
print(PozX, List[PozX])
...
...