Thuta Learning
ExercisesData & Databasesintermediate

Practice: Arrays & Indexing

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Practice Arrays & Indexing hands-on
  • Reinforce skills you've already learned through practice
  • Get comfortable finding and fixing your own mistakes

Take a moment to think about this

This lesson doesn't introduce anything new — it's meant to be a hands-on review of array creation, attributes, indexing, slicing, and basic operations from the tutorial's Basic chapter, through 4 short practice tasks. For each task, write the code yourself, predict the output, and then run it to check. These fundamentals are the base you'll need for the tougher broadcasting and aggregation tasks in Part 2.

Practice tasks

Task 1: Create a 1D array with your 5 favorite numbers and print its dtype and shape. Task 2: Reshape np.arange(12) into a (3, 4) 2D array, then use slicing to pull out the middle 2x2 block (rows 1-2, columns 1-2). Task 3: Create an array of even numbers from 0 to 20, then reverse it using slicing with step -1. Task 4: Add 5 and multiply by 2, element-wise, on an array, then print the result side by side with the original array to see the difference.

Example Code

python
import numpy as np

# Task 1: 1D array + attributes
favorites = np.array([7, 14, 21, 42, 100])
print("dtype:", favorites.dtype, "shape:", favorites.shape)

# Task 2: reshape + middle block slicing
grid = np.arange(12).reshape(3, 4)
print("grid:\n", grid)
middle_block = grid[0:2, 1:3]  # TODO: adjust to get the correct middle block
print("middle block:\n", middle_block)

# Task 3: even numbers, reversed with slicing
evens = np.arange(0, 21, 2)
reversed_evens = evens[::-1]
print("evens:", evens)
print("reversed:", reversed_evens)

# Task 4: element-wise operations
original = np.array([1, 2, 3, 4, 5])
transformed = (original + 5) * 2
print("original:", original)
print("transformed:", transformed)
You should see
For all 4 tasks, the print statements will correctly show the dtype/shape, the sliced 2x2 block, the reversed even-number array, and the transformed array.

Try it in 5 minutes

Try reshaping Task 2's grid to (4, 3) instead, and think through whether you'd need to adjust the middle-block slicing indexes (5 minutes).

A quick word of caution

For 2D slicing like grid[0:2, 1:3], the row range and column range need to be separated by a comma — writing it as double brackets like grid[0:2][1:3] can give you completely different logic.

Easy traps

  • Trying to reshape() without matching the total element count (rows x columns) to the original array size, triggering a ValueError
  • Assuming the stop index in slicing [start:stop] is inclusive and ending up missing one element (the stop index is actually exclusive)

Try it yourself

Try reshaping Task 2's grid to (4, 3) instead, and think through whether you'd need to adjust the middle-block slicing indexes (5 minutes).

You'll know it worked when: For all 4 tasks, the print statements will correctly show the dtype/shape, the sliced 2x2 block, the reversed even-number array, and the transformed array.