Editor

Slice and Dice

Intermediate · Write the code · from NumPy Arrays: Create, Index, Slice, and Reshape

From the lesson: NumPy Arrays: Create, Index, Slice, and Reshape

Imagine you have a spreadsheet with a million rows of numbers and you need to multiply every single value by 2. With a regular Python list, you'd write a loop that visits each cell one by one. It works, but it's slow.

Read the lesson

Given arr = np.arange(10, 100, 10) (which is [10, 20, 30, 40, 50, 60, 70, 80, 90]), print:

1. The first three elements

2. The last four elements

3. Every other element starting from the second (index 1)

import numpy as np
arr = np.arange(10, 100, 10)
# Print the three slices

More NumPy Array exercises

All Python exercises · 50 practice problems · Data Science & Machine Learning quiz