PythiloLesson 1 of 4 · progress kept in this browser

NumPy arrays

A NumPy array is a block of same-typed numbers. Operations apply to every element, in compiled code, without a Python loop.

import numpy as np

a = np.array([1, 2, 3, 4])
print(a * 10)     # [10 20 30 40]
print(a.dtype, a.shape)  # int64 (4,)
print(a[a > 2])   # [3 4]

m = np.arange(6).reshape(2, 3)
print(m[1, 2])    # row 1, column 2
print(m[:, 0])    # first column

a > 2 produces an array of booleans, known as a mask. Using it as an index keeps the matching elements.

Your task

Write grid(rows, cols) returning an int array of the numbers from 0 in that shape. Write last_column(matrix) returning the final column. Write above_mean(values) returning the elements strictly above the mean. Write zero_negatives(values) returning a copy with negatives set to 0, leaving the input unchanged.

The first run loads NumPy into this browser.