Run NumPy Online

NumPy Arrays: shape, size and dtype

Create your first NumPy array and learn the three properties you will check all the time: shape, size and dtype.

  • Intermediate
  • 2 min read
  • 1 runnable example
What you'll learn
  • Creating arrays with np.array
  • shape, ndim, size and dtype
  • Why NumPy is faster than lists
shape (2, 3)
1[0, 0]
2[0, 1]
3[0, 2]
4[1, 0]
5[1, 1]
6[1, 2]
values[1, 1] is 5: row 1, column 1.

One type, many dimensions

The problem

Python lists are flexible but slow for maths on many numbers: adding 1 to a million values needs a Python loop. Science, data analysis and machine learning work with large grids of numbers, and they need a structure built for fast calculations on all values at once.

Think of it like this

A NumPy array is like a sheet of graph paper where every square holds the same kind of number. Because every square is the same size and type, you can work on a whole row, a column or the entire sheet in one go, instead of visiting squares one by one.

From graph paper to code
np.array([[1, 2, 3], [4, 5, 6]])📄 A sheet with 2 rows and 3 columns
values.shape📐 Its size: (2, 3)
values.size🔢 How many squares: 6
print(values)👀 The whole sheet, row by row
Same type in every cell, so whole-array operations are fast.
In one sentence

A NumPy array (ndarray) is a grid of values of one data type with a fixed shape, designed for fast operations on all elements at once.

Key points

  1. np.array([[1, 2, 3], [4, 5, 6]]) turns a list of lists into a 2-dimensional array.
  2. values.shape is (2, 3): two rows and three columns.
  3. values.size counts all elements: 6.
  4. Every element has the same type, which is what makes array maths fast.

When should you use it?

  • Calculations on many numbers at once: scores, measurements, prices.
  • Images, which are arrays of pixel values.
  • The input format for most data science and machine learning libraries.

When not to use it

  • Mixed data such as names and numbers together: a pandas DataFrame or a list of dictionaries fits better.
  • Collections that grow item by item: appending to an array copies it every time, so build a list first.

See how it works

Point at a cell to see its row and column, and where it sits in memory, where all rows are stored one after another.

Example code

Run code →
main.py
import numpy as np

values = np.array([[1, 2, 3], [4, 5, 6]])

print(values)
print("shape:", values.shape)
print("size:", values.size)
Get a Run button for this example

Result of running the code

[[1 2 3]
 [4 5 6]]
shape: (2, 3)
size: 6

Code explanation

The array is two-dimensional with two rows and three columns. Shape describes its dimensions and size counts all values.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input shape and dtype.
  2. Try negative, floating-point, and missing values.
  3. Apply the operation to larger multidimensional arrays and measure performance.
Run in the Python compiler →

Common mistakes

Ragged rows

Rows of different lengths do not make a proper 2-D array.

Problem
np.array([[1, 2, 3], [4, 5]])  # error or object array
Fix
np.array([[1, 2, 3], [4, 5, 6]])

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges