π This article was originally published on MAK Studio . Check out the full interactive lesson with live demos and code examples.
Home βΊ
Courses βΊ
Python βΊ
Problems βΊ
Slicing
π Concept Β· 4 min read
Python slicing. Indices point between elements.
The a[start:stop:step] syntax is elegant once you understand the mental model. The key insight: indices point between elements, not at them. The start index is the boundary before the first element you want; the stop index is the boundary after the last. That's why a[1:4] is three elements, not four. Once you see the boundaries, 90% of slicing confusion disappears.
Short answer: a[start:stop:step] β start inclusive, stop exclusive, step defaults to 1. a[1:4] is elements 1, 2, 3. a[:] copies. a[-3:] is the last 3. a[::2] takes every other. a[::-1] reverses. Slicing returns a new sequence β it doesn't modify the original. Negative indices count from the end. Missing values default to the ends (or to 1 for step).β¦