Data Structures16 min read

Indexing and Slicing

Learn indexing, slicing, and step slicing for lists/tuples/strings with strong intuition, so you stop guessing and start slicing confidently.

David Miller
December 21, 2025
0.0k0

Indexing and slicing are core skills for working with sequences: - list - tuple - string ## Indexing recap ```python data = ["a", "b", "c", "d"] print(data[0]) # a print(data[-1]) # d ``` ## Slicing format `data[start:end]` includes start but excludes end. ```python data = ["a", "b", "c", "d", "e"] print(data[1:4]) # ['b','c','d'] ``` ## Slice with step `data[start:end:step]` ```python nums = [0,1,2,3,4,5,6,7,8,9] print(nums[0:10:2]) # [0,2,4,6,8] ``` ## Reverse a sequence ```python print(nums[::-1]) ``` ## Common beginner mistakes - forgetting end is exclusive - mixing up negative indexes ## Graph: slicing mental model ```mermaid flowchart LR A[Sequence] --> B[start at index] B --> C[stop before end] C --> D[optional step] ``` ## Remember - end index is not included - step helps skip and reverse - slicing works across list/tuple/string

#Python#Beginner#Core Skills