Skip to content
Learn Netverks

Lesson

Step 15/36 42% through track

slicing-indexing

Slicing and indexing

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Slicing and indexing: the syntax, patterns, and safety habits you need before advancing in Python.

Teams still ship Slicing and indexing in Python codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Slicing and indexing in contexts like: Scripts, Django/FastAPI apps, notebooks, and glue code between systems.

Write Python 3 in the editor and click Run on server—the dev runner executes your script with print() for output; stdlib only in playground snippets (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Sequences (str, list, tuple) support indexing and slicing with start:stop:step. Slices return new objects—strings and tuples stay immutable; list slices are shallow copies of the slice range.

Indexing and slices

items = [0, 1, 2, 3, 4]
print(items[0], items[-1])
print(items[1:4])   # [1, 2, 3]
print(items[::2])   # every second
print("hello"[::-1])  # reverse

Slice assignment (lists only)

nums = [1, 2, 3, 4]
nums[1:3] = [20, 30]
print(nums)

Important interview questions and answers

  1. Q: Slice bounds?
    A: stop is exclusive; out-of-range indices on slices clamp—unlike single indexing which raises IndexError.
  2. Q: [::-1] effect?
    A: Reverses the sequence—works on strings without mutating them.

Self-check

  1. What does [1:4] include from index 1?
  2. How reverse a string without mutating?

Tip: Slice [start:stop:step]—stop is exclusive; negative indices count from the end.

Interview prep

Slice stop exclusive?

a[1:4] includes indices 1,2,3—not 4—unlike some inclusive APIs in other languages.

Negative indices?

-1 is last element; slices clamp out-of-range without IndexError.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Negative index?
  • Step syntax?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump