summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-03-25 22:40:28 (GMT)
committerGitHub <noreply@github.com>2024-03-25 22:40:28 (GMT)
commit9359fdd6fa6cdba4e046fdb56ec12abbea2ded1a (patch)
tree225fd9dab99e917a62379cf03879f343a0fb98f7 /Doc
parent0c334f627bc11b11193ee7224971e4a249f24fe4 (diff)
downloadcpython-9359fdd6fa6cdba4e046fdb56ec12abbea2ded1a.zip
cpython-9359fdd6fa6cdba4e046fdb56ec12abbea2ded1a.tar.gz
cpython-9359fdd6fa6cdba4e046fdb56ec12abbea2ded1a.tar.bz2
[3.12] Add information about negative indexes to sequence datamodel doc (GH-110903) (#117238)
Co-authored by Terry Jan Reedy (cherry picked from commit c2276176d543a2fc2d57709c2787f99850fbb073) Co-authored-by: Adorilson Bezerra <adorilson@gmail.com>
Diffstat (limited to 'Doc')
-rw-r--r--Doc/reference/datamodel.rst9
1 files changed, 6 insertions, 3 deletions
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
index e41697d..afe1381 100644
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -299,14 +299,17 @@ Sequences
These represent finite ordered sets indexed by non-negative numbers. The
built-in function :func:`len` returns the number of items of a sequence. When
the length of a sequence is *n*, the index set contains the numbers 0, 1,
-..., *n*-1. Item *i* of sequence *a* is selected by ``a[i]``.
+..., *n*-1. Item *i* of sequence *a* is selected by ``a[i]``. Some sequences,
+including built-in sequences, interpret negative subscripts by adding the
+sequence length. For example, ``a[-2]`` equals ``a[n-2]``, the second to last
+item of sequence a with length ``n``.
.. index:: single: slicing
Sequences also support slicing: ``a[i:j]`` selects all items with index *k* such
that *i* ``<=`` *k* ``<`` *j*. When used as an expression, a slice is a
-sequence of the same type. This implies that the index set is renumbered so
-that it starts at 0.
+sequence of the same type. The comment above about negative indexes also applies
+to negative slice positions.
Some sequences also support "extended slicing" with a third "step" parameter:
``a[i:j:k]`` selects all items of *a* with index *x* where ``x = i + n*k``, *n*