PY-401 ยท NumPy foundations
NumPy arrays
โฑ 9 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
A NumPy array is a grid of values that all have the same type, stored side by side in memory so maths on millions of them runs fast. You make one with np.array([...]), and every array knows its shape (rows and columns), ndim (number of dimensions) and dtype (the type of every value). It is the building block under pandas, matplotlib and almost every Python AI library.
- Create an array from a list
- Read shape, ndim and dtype
- Make arrays with zeros, arange and linspace
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
import numpy as npprices = np.array([120.0, 80.5, 99.9, 45.0])print(prices)print(prices.dtype, prices.shape, prices.ndim)print(np.zeros((2, 3)))print(np.arange(0, 10, 2))print(np.linspace(0, 1, 5))
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
[120. 80.5 99.9 45. ]
float64 (4,) 1
[[0. 0. 0.]
[0. 0. 0.]]
[0 2 4 6 8]
[0. 0.25 0.5 0.75 1. ] ๐ฏ Turn the step counts into a NumPy array called steps, make days = np.arange(1, 8) (the numbers 1 to 7), and print steps.shape.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
steps = np.array([...]) then days = np.arange(1, 8) then print(steps.shape)
- The problemA shop has 2 million sales rows. Adding 18% tax to each price with a Python for loop takes seconds. With a NumPy array it is one line and finishes before you blink.
- The Python idea
import numpy as npA NumPy array is a grid of values that all have the same type, stored side by side in memory so maths on millions of them runs fast. - In real lifeA fitness app stores a year of step counts as one array of 365 numbers. The weekly chart, the monthly average and the "best day" badge all come from that one array.
Start with a question
A shop has 2 million sales rows. Adding 18% tax to each price with a Python for loop takes seconds. With a NumPy array it is one line and finishes before you blink.
Data science is maths on many numbers at once. NumPy does that maths in fast compiled code, and pandas tables are NumPy arrays underneath.
Explain it like I'm 10
A Python list is a bag where you can throw in anything: a toy, a sock, a number. A NumPy array is an egg box: every slot is the same size and holds the same kind of thing, so you can check all the eggs at once.
The technical explanation
Start with import numpy as np. Everyone uses the short name np, so every example you find online matches yours.
np.array([120.0, 80.5]) copies a list into an array. A list of lists becomes a 2-D array: rows and columns.
.shape is a tuple: (4,) is 4 values in a row, (2, 3) is 2 rows of 3. .ndim counts the dimensions. .dtype is the type, such as float64 or int64.
np.zeros((2, 3)) makes a 2 by 3 grid of 0.0, np.arange(0, 10, 2) works like range but returns an array, and np.linspace(0, 1, 5) gives 5 evenly spaced numbers from 0 to 1, end included.
| Python list | NumPy array | |
|---|---|---|
Types inside | Anything, mixed | One type for all |
Maths on all items | Needs a loop | One expression |
Speed on a million numbers | Slow | Fast (compiled code) |
Size can change | Yes, append | No, make a new array |
Syntax
import numpy as np
a = np.array([1.5, 2.0, 3.5])
a.shape, a.ndim, a.dtype
np.zeros((rows, cols))
np.arange(start, stop, step)
np.linspace(start, stop, count) Real-world scenario
A fitness app stores a year of step counts as one array of 365 numbers. The weekly chart, the monthly average and the "best day" badge all come from that one array.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Indexing and slicing arrays โQuestions people ask
What is the difference between a Python list and a NumPy array?
A list can hold mixed types and grow with append. A NumPy array holds one type in a fixed-size block of memory, so maths on every value runs in fast compiled code without a Python loop.
Why is NumPy faster than a Python loop?
The loop over the values runs inside NumPy's compiled C code instead of the Python interpreter, and the numbers sit next to each other in memory, so the CPU can process them in bulk.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner What is the difference between a Python list and a NumPy array?
A list can hold mixed types and grow with append. A NumPy array holds one type in a fixed-size block of memory, so maths on every value runs in fast compiled code without a Python loop.
beginner Why is NumPy faster than a Python loop?
The loop over the values runs inside NumPy's compiled C code instead of the Python interpreter, and the numbers sit next to each other in memory, so the CPU can process them in bulk.
intermediate How does almost everyone import NumPy?
import numpy as np. np is the shared convention, so examples everywhere look the same.
intermediate a = np.zeros((3, 4)). What is a.shape?
(3, 4). shape is (rows, columns): 3 rows of 4.
Quick revision
Remember
- Create an array from a list
- Read shape, ndim and dtype
- Make arrays with zeros, arange and linspace
- Every value in an array has the same type.
np.array([1, 2, "3"])quietly turns all three into text. Check.dtypewhen numbers come from a file.
Syntax
import numpy as np
a = np.array([1.5, 2.0, 3.5])
a.shape, a.ndim, a.dtype
np.zeros((rows, cols))
np.arange(start, stop, step)
np.linspace(start, stop, count) Official documentation
- NumPy: the absolute basics for beginners (docs.python.org)
- Array creation (docs.python.org)