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.
- Creating arrays with np.array
- shape, ndim, size and dtype
- Why NumPy is faster than lists
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.
np.array([[1, 2, 3], [4, 5, 6]])📄 A sheet with 2 rows and 3 columnsvalues.shape📐 Its size: (2, 3)values.size🔢 How many squares: 6print(values)👀 The whole sheet, row by rowA 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
np.array([[1, 2, 3], [4, 5, 6]])turns a list of lists into a 2-dimensional array.values.shapeis(2, 3): two rows and three columns.values.sizecounts all elements: 6.- 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
import numpy as np
values = np.array([[1, 2, 3], [4, 5, 6]])
print(values)
print("shape:", values.shape)
print("size:", values.size)
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.
- Change the input shape and dtype.
- Try negative, floating-point, and missing values.
- Apply the operation to larger multidimensional arrays and measure performance.
Common mistakes
Ragged rows
Rows of different lengths do not make a proper 2-D array.
np.array([[1, 2, 3], [4, 5]]) # error or object array
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.
- Dot Product of Two Vectors Multiply two vectors element by element and add the results: the core operation of every neuron. Easy
- Sum an Array Add up all values in a list. Easy
- Activation Functions Apply sigmoid, ReLU, Leaky ReLU or tanh to every value in a list. Easy