Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e64c7d3

Browse files
[3.11] Add information about negative indexes to sequence datamodel doc (GH-110903) (#117239)
Co-authored by Terry Jan Reedy (cherry picked from commit c227617) Co-authored-by: Adorilson Bezerra <[email protected]>
1 parent 6261322 commit e64c7d3

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Doc/reference/datamodel.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,14 +299,17 @@ Sequences
299299
These represent finite ordered sets indexed by non-negative numbers. The
300300
built-in function :func:`len` returns the number of items of a sequence. When
301301
the length of a sequence is *n*, the index set contains the numbers 0, 1,
302-
..., *n*-1. Item *i* of sequence *a* is selected by ``a[i]``.
302+
..., *n*-1. Item *i* of sequence *a* is selected by ``a[i]``. Some sequences,
303+
including built-in sequences, interpret negative subscripts by adding the
304+
sequence length. For example, ``a[-2]`` equals ``a[n-2]``, the second to last
305+
item of sequence a with length ``n``.
303306

304307
.. index:: single: slicing
305308

306309
Sequences also support slicing: ``a[i:j]`` selects all items with index *k* such
307310
that *i* ``<=`` *k* ``<`` *j*. When used as an expression, a slice is a
308-
sequence of the same type. This implies that the index set is renumbered so
309-
that it starts at 0.
311+
sequence of the same type. The comment above about negative indexes also applies
312+
to negative slice positions.
310313

311314
Some sequences also support "extended slicing" with a third "step" parameter:
312315
``a[i:j:k]`` selects all items of *a* with index *x* where ``x = i + n*k``, *n*

0 commit comments

Comments
 (0)