Skip to content
LearnPY
NumPy

NumPy arrays: one type, many values

Meet the array: a fixed-type block of values that supports whole-array arithmetic, slicing, and shape-aware operations.

8 min readbeginnernumpy

By the end

  • Create arrays from Python lists and NumPy constructors.
  • Read an array's shape, dimensions, and dtype.
  • Index, slice, and filter arrays by position or condition.
  • Explain why array arithmetic differs from list arithmetic.

Why arrays exist

A Python list is wonderfully flexible: it can hold numbers, strings, nested lists, and objects in the same container. That flexibility costs memory and speed. A NumPy array makes the opposite trade: every value has the same type, and the values sit in one compact block. In return, NumPy can operate on the whole block at once.

Python

The same multiplication, two different meanings

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

Multiplying a list repeats it. Multiplying an array scales every element. This is vectorized arithmetic: you describe the operation on the whole array, and NumPy handles the loop in compiled code.

Creating arrays

Most lessons start with np.array(...), but NumPy also has constructors for common shapes. zeros and ones fill a shape with a starting value, arange builds evenly spaced ranges, and linspace asks for a specific number of points between two endpoints.

Python

Four common constructors

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

  • np.zeros(shape) and np.ones(shape) are useful placeholders before a loop or calculation fills them.
  • np.arange(start, stop, step) excludes the stop value, like range().
  • np.linspace(start, stop, count) includes both endpoints and gives you a chosen number of points.

Shape, dimensions, and axes

Shape tells you how many values sit along each axis. For a table-like array, axis 0 runs down the rows and axis 1 runs across the columns. Reduction methods such as sum accept an axis argument, which chooses the direction that gets collapsed.

Python

A 2 by 3 grid and its reductions

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

grid.sum(axis=0) collapses the rows and returns one total per column: [5, 7, 9]. grid.sum(axis=1) collapses the columns and returns one total per row: [6, 15]. When an axis is collapsed, it disappears from the result's shape.

ExpressionResultWhat happened
grid.shape(2, 3)2 rows, 3 columns
grid.ndim2Two axes
grid.sum(axis=0)[5, 7, 9]Columns collapsed
grid.sum(axis=1)[6, 15]Rows collapsed
Reading the grid

Indexing, slicing, and masks

Array indexing starts at zero, just like a list. Slices keep the start and drop the stop. The difference is that a NumPy slice returns another array, and you can use a boolean array as an index to keep only the positions where the condition is true.

Python

Position, slice, step, and mask

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

  • values[1] selects one value and returns a NumPy scalar.
  • values[1:4] selects positions 1, 2, and 3; the stop index is excluded.
  • values[::2] takes every second value from start to finish.
  • values[values > 15] keeps the entries where the mask is True.

One dtype for the whole array

Because every slot has the same size, NumPy stores a single dtype for the array. When you mix integers and floats, the result has to promote to a type that can hold both. You can inspect the dtype and convert explicitly with astype.

Python

Promotion and explicit conversion

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

The decimal values forced the whole array to float64. astype("int64") then created a new integer array, truncating toward zero. The default integer width can vary by platform, including WebAssembly, so name a dtype when exact storage matters.

Putting it together

A short analysis usually combines construction, a mask, an aggregation, and vectorized arithmetic. The example below filters a week of temperatures, computes an average, and standardizes the values so each one describes its distance from the mean in standard deviations.

Python

Filter, summarize, and standardize

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

Notice that none of these steps needed an explicit loop. The array carries its shape through each operation, and NumPy applies the arithmetic element by element. The next lesson extends that idea to arrays with different but compatible shapes.

Practice

Make it stick

Run the tests as often as you like. Hints and the solution are available when you need them.

Exercise

Convert the Celsius readings to Fahrenheit with vectorized array arithmetic. Keep the result as a NumPy array.

Your solution

Ready
Loading editor…

Results

Run the tests to check your solution. The first run downloads Python; later runs reuse the session.

Quiz

Check your understanding

01What does np.array([1, 2, 3]) * 2 produce?
02What is np.ones((3, 2)).shape?
03For a 2D array, what does grid.sum(axis=0) return?

Lesson progress