Python Programming · High School (11–12) · 1 course, 10 worked problems, 20 practice questions
NumPy is a Python library for working with many numbers at once. Its main tool is the array, a grid of values of one type. You make one with np.array(), np.arange() or np.linspace(). Arithmetic on an array acts on every element, so v = u + a * t finds the speed for a whole array of times in one line, with no loop. Functions such as np.mean(), np.max() and np.sum() summarise data, and slicing picks out parts of it. Arrays are fast, and they sit under most scientific tools in Python.
np.array(), np.arange() and np.linspace()Choose an answer, then check it. Each one shows a full explanation.
Counting in steps: what does print(np.arange(2, 11, 3)) show?
One number from many: what is the mean that print(np.array([1, 2, 3]).mean()) shows?
t = np.array([[1, 2, 3], [4, 5, 6]]). What is t.shape?
Limits on every value: what does print(np.clip(np.array([-5, 3, 12]), 0, 10)) show?
Each one has a hint, the key idea and a full worked solution.
Which function gives exactly 50 equally spaced numbers from 0 to 1, with both ends included?
Why is it faster to add two big arrays with a + b than to add their numbers in a Python loop?
What does np.array([1, 2]) * np.array([3, 4]) give?
a is an array of numbers. Which line picks the numbers that are from 10 to 20, both ends included?
A table has one row for each student and one column for each test. Which call gives one average for each student?
A student writes b = a[:3] and then b[0] = 99. The student is surprised that a[0] is 99 too. What is the reason?