Python Tutorial 0/52 lessons ~6 min read Lesson 22

    Slicing & Indexing

    Slicing with [start:stop:step] works on any sequence: list, tuple, string.

    Course progress0%
    Focus
    8 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Slicing with [start:stop:step] works on any sequence: list, tuple, string. Negative indices count from the end.

    Understanding the topic

    Core concepts to understand:

    • xs[1:4] — items 1..3.
    • xs[::-1] — reverse.
    • xs[::2] — every other.
    • Slicing copies; reassignment slices replace in place.

    Syntax reference

    Visual flow / code:

    python
    xs = [0, 1, 2, 3, 4, 5]
    xs[1:4] # [1, 2, 3]
    xs[:3] # [0, 1, 2]
    xs[-2:] # [4, 5]
    xs[::-1] # reversed
    xs[::2] # [0, 2, 4]
    # Replace a slice
    xs[1:3] = [10, 20, 30] # grows the list
    # Strings slice the same way
    "abcdef"[1:4] # 'bcd'

    Execution workflow

    1Slicing & Indexing Workflow
    1 / 4

    Step 1

    xs[1:4] — items 1..3.

    Apply this step while implementing slicing & indexing in real code.

    Real-world use

    Slicing is one of Python's most loved features — it makes data wrangling code read like English: data[-100:], signal[::2].

    Best practices

    • Use slicing for copies: new = old[:].
    • Step -1 reverses any sequence.

    Hands-on exercise

    Interview preparation — practice these questions:

    • What does xs[::-1] do?
    • Slice vs index — what's the difference?
    • Slice assignment behavior?

    Summary

    In summary: [start:stop:step] is universal. Negative indices count from end.

    Ready to mark this lesson complete?Track your journey across the entire course.