Indexing and Slicing Arrays
Grabbing rows, columns, and ranges.
Indexing and Slicing Arrays is a free Data Science Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Grab the Pieces You Need
Real analysis means pulling out parts of your data. NumPy's indexing lets you grab single values, rows, columns, or ranges with ease. ✂️
Single Elements by Position
Just like a list, you reach one element by its index. Counting starts at zero, so arr[0] is the very first value.
arr = np.array([10, 20, 30])
arr[0] # 10Count From the End
Negative indices count backward. arr[-1] hands you the last element without needing to know the array's length.
arr[-1] # 30Slices Grab a Range
A slice uses start:stop to pull a range. The start is included, the stop is not, just like Python lists.
np.arange(10)[2:5] # 2 3 4Steps Inside a Slice
Add a third number for a step. Using ::2 keeps every other element, an easy way to thin out data.
np.arange(10)[::2] # 0 2 4 6 8Two Dimensions, One Bracket
For a 2D array, give row and column in one bracket: arr[row, col]. This is cleaner than chaining two sets of brackets.
m = np.array([[1, 2], [3, 4]])
m[1, 0] # 3Grab a Whole Row
A lone colon means take everything along that axis. So m[0, :] returns the entire first row.
m[0, :] # array([1, 2])Grab a Whole Column
Flip it to pull a column. m[:, 0] sweeps every row and keeps just the first column's values.
m[:, 0] # array([1, 3])Slices Are Views, Not Copies
Careful: a slice is a view into the original. Change the slice and the source array changes too, which can surprise you.
s = arr[0:2]
s[0] = 99 # arr also changesMake a Real Copy
When you need independence, call copy. It hands back a separate array so edits never touch the original.
safe = arr[0:2].copy()Slice to Assign, Too
Indexing works on the left side as well. Assign to a slice and you update a whole range in one move.
arr[0:2] = 0 # first two become 0Quick Check
Let's test selecting a column from a 2D array.
Recap
You can now carve up arrays: index single values, slice ranges with steps, target rows and columns, and copy when you need to stay safe. 🎉
Frequently asked questions
Is the “Indexing and Slicing Arrays” lesson free?
Yes — the full text of “Indexing and Slicing Arrays” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.
What will I learn in “Indexing and Slicing Arrays”?
Grabbing rows, columns, and ranges. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Data Science Academy?
No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Indexing and Slicing Arrays” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Data Science Academy lesson?
Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- From Python List to ndarray
- Shape, Size, and dtype
- Vectorized Math Without Loops
- Indexing and Slicing Arrays