Lesson 1 · Unit 01
Arrays
Shapes, slices, masks and why loops disappear
What you practise
shape,dtype, creating (zeros,arange,linspace), indexing and slicing, boolean masks, views vs copies, why vectorized beats a Python loop
Try a question from this lesson
Pick one
For a Python list, [1, 2, 3] * 2 repeats the list. What does the same line do to a NumPy array?
import numpy as np
a = np.array([1, 2, 3])
print(a * 2)Correct answer
[2 4 6]
Why
Arithmetic on an array is applied to every element. No loop, no comprehension: a * 2 doubles each number and returns a new array. Repeating is what lists do.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
A grid of one type
An array is a grid of numbers that all have the same type, stored side by side in memory.
shape gives the count along each axis and dtype the kind of number. Whole-array arithmetic runs in one compiled loop, far faster than a Python for.
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6]])
print(a.shape) # (2, 3): rows, columns
print(a.dtype) # int64
print(a + 10) # every element at once11 more exercises in the app
- 4× type the answer
- 2× fill the blanks
- 3× multiple choice
- 1× true or false
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.