Skip to content

Clarify Python Tutorial Gap on Negative Step Slicing #137960

@NK5W

Description

@NK5W

Bug report

Bug description:

Negative/reverse slicing (_variable[_one:_two:-1]) is easy to misinterpret using only the two‑argument “indices between characters” model from the tutorial.

Example:

_variable = [1, 2, 3, 4]
print(_variable[2:0:-1])

Expected from two‑arg mental model: [2, 1]
Actual: [3, 2] — because:

  • Stop index is always excluded, even when step is negative.
  • Traversal starts at start and moves in step increments until it passes stop, never including stop.

The tutorial’s diagram is correct for step=1 but does not cover step at all, so readers extending that model to negative steps will predict the wrong result.

Proposed doc improvement: keep current explanation for two‑argument slices, but add a short note or link to the full slicing definition in the language reference showing how negative steps work, with a concrete example like [2:0:-1] → [3, 2]. This would prevent the confusion without overloading beginners.

CPython versions tested on:

3.15

Operating systems tested on:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirpendingThe issue will be closed if no feedback is provided

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions