NumPy Arrays
Meet NumPy and the ndarray: one fixed-type block of numbers, why it's fast, and how to build one from a list.
Picture this: you're training an AI to recognise photos, and each photo is a million pixel values. In core Python you'd stash them in a list, but a list is a loose bag: it can hold 5, 'hello', and True all at once, so Python has to re-check the type of every item every time it does math. Over a million numbers, that bookkeeping makes lists painfully slow.
Enter NumPy (short for Numerical Python), the library nearly every AI tool is built on. Its star is the ndarray (short for n-dimensional array): one solid block of numbers that are all the same type. Because NumPy knows every element is, say, a 64-bit number, it can crunch the whole block at machine speed, often 10 to 100 times faster than a Python loop. Under the hood, pandas and scikit-learn store their data as ndarrays, and deep-learning tools like PyTorch use array structures (called tensors) modeled directly on them, so this is the foundation the whole AI stack stands on.
import numpy as np
arr = np.array([1, 2, 3])
print(arr) # [1 2 3]
print(type(arr)) # <class 'numpy.ndarray'>We wrote import numpy as np, giving the library the short nickname np. You'll see np in literally every NumPy tutorial, notebook, and codebase on earth; it's a universal convention, so use it too.
You build an array from a Python list and print it. What shows up?
import numpy as np
nums = np.array([10, 20, 30])
print(nums)- A[10 20 30]
- B[10, 20, 30]
- Carray([10, 20, 30])
- D10 20 30
Here's the rule that makes arrays fast: every element shares one data type. NumPy calls that shared type the dtype (short for data type), think int64 for whole numbers or float64 for decimals. A NumPy array is like a tray of ice-cube slots where every cube is made of the same material; a Python list is more like a junk drawer where each slot holds anything.
So what if you mix a whole number and a decimal in one array? NumPy refuses to keep two materials in one tray; it quietly promotes everything to the more general one, float64:
mix = np.array([1, 2, 3.5])
print(mix) # [1. 2. 3.5]
print(mix.dtype) # float64The 1 and 2 became 1. and 2., both floats, so the whole tray is one material.
What dtype does np.array([1, 2, 3]) have, and why?
- Aint64, because every value is a whole number, so NumPy picks one integer type for all of them
- Bfloat64, because NumPy always stores numbers as decimals
- Cobject, because it keeps each number's own type separately
- DIt has no dtype until you do math on it
One decimal sneaks into a list of whole numbers. What does NumPy print?
import numpy as np
a = np.array([4, 5, 6.0])
print(a)- A[4. 5. 6.]
- B[4 5 6]
- C[4, 5, 6.0]
- D[4 5 6.0]
Why can NumPy do math on a million-number array so much faster than a Python for loop over a list?
- AEvery element shares one dtype, so NumPy skips per-item type checks and runs one pre-compiled routine over the whole block
- BNumPy secretly uses more CPU cores for every operation
- CLists are slow because they can't hold numbers at all
- DNumPy rounds the numbers off to make them smaller
Fill in the blank so this turns a Python list into a NumPy array. Write just the missing function name (without np.).
import numpy as np
scores = np.____([90, 85, 100])- NumPy (Numerical Python) is the number-crunching library the whole AI stack is built on
- Its core type is the ndarray, one fixed-type block of numbers, which is why it runs 10-100x faster than a Python loop
- Every element shares one dtype; mix an int and a float and NumPy promotes them all to
float64 np.array([...])builds an array from a Python list; import the library asnpby convention- Arrays print with spaces and no commas:
[1 2 3](a giveaway that it's an array, not a list)
Next up: the shortcut builders, how to make an array of a thousand zeros without typing a single one.