✦ Osnove Pythona Kolekcije

Concept 07 · Indexing & Slicing

Pronađite jednu stavku.
Take a useful slice.

Indexes select one list item; slices select an ordered part of a list.

01 Uči02 Isprobaj03 Predvidi

Indeksi počinju od nule, a izdvojeni deo se završava pre krajnje granice.

Use square brackets after a list name to access an item. Positive indexes count from the start; negative indexes count backward from the end.

numbers = [10, 20, 30, 40, 50]
print(numbers[0])
print(numbers[-1])

numbers[0] is the first item and numbers[-1] is the final item.

10 50

A slice uses start:stop:step. The start is included, but the stop position is not included.

↳

Granica izdvajanja: numbers[1:4] contains positions 1, 2, and 3, but not position 4.

Uzmite svaku drugu stavku
numbers = [10, 20, 30, 40, 50]
print(numbers[::2])

An omitted start and stop use the full list; step 2 selects every second item.

Python radni prostor

Učitavanje Python okruženja…

Try different indexes and slice boundaries. Predict the result before running the code.

indexing_slicing.pyPython
IzlazČeka se Python
Okruženje pregledača se učitava. I dalje možeš da menjaš primer koda.

Pyodide pokreće Python u Web Worker-u pregledača. Zaustavlja se i ponovo pokreće ako se kod predugo izvršava. Izvršavanje u pregledaču nije potpuna bezbednosna granica; ne unosi tajne i ne oslanjaj se na njega za izolaciju nepouzdanog koda.

Šta će ovaj program ispisati?

The stop index is not included in a slice.

letters = ["a", "b", "c", "d", "e"]
print(letters[1:4])
Izaberi izlaz
Sloby, maskota Python AI Tutora

Tvoj mentor za učenje

Ask Sloby about indexing and slicing

Describe what feels unclear about positions, negative indexes, or slice boundaries.

Zdravo! Ja sam Sloby. Hajde da zajedno pratimo indekse i izdvajanja iz liste.

What will this program print, and why? Explain the negative index and the slice.

scores = [12, 24, 36, 48, 60]

print(scores[-2])
print(scores[1:4])
Python AI Tutor · Uči razumevanjem, a ne samo pokretanjem koda.