✦ Python Foundations Collections module

Concept 07 · Indexing & Slicing

Find one item.
Take a useful slice.

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

01 Learn02 Try03 Predict

Indexes begin at zero, and slices stop before the end.

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.

↳

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

Take every second item
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 Playground

Loading Python runtime…

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

indexing_slicing.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

The stop index is not included in a slice.

letters = ["a", "b", "c", "d", "e"]
print(letters[1:4])
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Ask Sloby about indexing and slicing

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

Hi! I'm Sloby. Let's trace list indexes and slices together.

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 · Learn by understanding, not just by running code.