diff options
author | 2022-11-02 14:23:20 +0000 | |
---|---|---|
committer | 2022-11-02 14:23:20 +0000 | |
commit | 1c1810b8e14b1e9c1f008123d716dad7fba00f99 (patch) | |
tree | 531c6ed6edd65ade28841ff00e15095037397ae2 | |
parent | Merge pull request #2311 from python-discord/mbaruh-bump-psql (diff) | |
parent | Merge branch 'main' into Keyacom-patch-1 (diff) |
Merge pull request #2285 from Keyacom/Keyacom-patch-1
-rw-r--r-- | bot/resources/tags/slicing.md | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/bot/resources/tags/slicing.md b/bot/resources/tags/slicing.md new file mode 100644 index 000000000..717fc46b7 --- /dev/null +++ b/bot/resources/tags/slicing.md @@ -0,0 +1,24 @@ +--- +aliases: ["slice", "seqslice", "seqslicing", "sequence-slice", "sequence-slicing"] +embed: + title: "Sequence slicing" +--- +*Slicing* is a way of accessing a part of a sequence by specifying a start, stop, and step. As with normal indexing, negative numbers can be used to count backwards. + +**Examples** +```py +>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] +>>> letters[2:] # from element 2 to the end +['c', 'd', 'e', 'f', 'g'] +>>> letters[:4] # up to element 4 +['a', 'b', 'c', 'd'] +>>> letters[3:5] # elements 3 and 4 -- the right bound is not included +['d', 'e'] +>>> letters[2:-1:2] # Every other element between 2 and the last +['c', 'e'] +>>> letters[::-1] # The whole list in reverse +['g', 'f', 'e', 'd', 'c', 'b', 'a'] +>>> words = "Hello world!" +>>> words[2:7] # Strings are also sequences +"llo w" +``` |