Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 49 ยท Getting Started

What Is NumPy?

Meet NumPy, the array library behind Python data science. See why ndarrays beat lists for numbers, run your first import, and learn where NumPy fits.

Python lists are flexible, but they are not built for heavy number work. Each item in a list is a full Python object with its own header, type and reference count, and every + in a loop is interpreted one step at a time. Add a million numbers that way and you pay for a million little detours.

NumPy (short for Numerical Python) fixes this with one core type, the ndarray: a block of values that all share the same type and sit side by side in memory. Math on an ndarray runs in compiled C code, so one line like a * 2 touches every element without a Python loop. In this lesson you will meet the idea, run your first import, compare arrays with lists, and see where NumPy sits in the wider Python data world.

If you want a short overview first, the lesson NumPy Basics in the Python tutorial covers the essentials in one page. This tutorial goes much deeper, one topic at a time.

Why arrays instead of lists

A list can hold anything: [1, "two", 3.0, None]. That freedom costs memory and speed. An ndarray makes a trade: every element has the same type (its dtype), and in return it gets a compact layout and fast operations.

graph LR
    subgraph L["Python list"]
    L0["slot 0"] --> O0["int object"]
    L1["slot 1"] --> O1["int object"]
    L2["slot 2"] --> O2["int object"]
    end
    subgraph A["NumPy array"]
    N["1 | 2 | 3<br/>raw numbers, side by side"]
    end

A list stores pointers to separate objects; an array stores the numbers themselves in one block.

Three things follow from that layout:

  • Less memory. An array of 64-bit integers uses 8 bytes per element. A list of ints uses an 8-byte pointer plus a separate Python integer object for each item.
  • Faster math. Operations loop in C over the contiguous block, and many use CPU vector instructions.
  • Cleaner code. You write prices * 1.2 instead of a loop or a comprehension. This style is called vectorization, and Lesson Vectorized Operations and ufuncs covers it fully.

Importing NumPy

Everyone imports NumPy under the short name np. You will see this in almost every data-science project, so use it too:

Python
import numpy as np

NumPy is a third-party package. On your own computer you install it with pip install numpy. The editors on this page load it automatically when they see import numpy; the first run downloads it, so it may take a few extra seconds.

Python Your first array

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the output of a * 2. With a list, [10, 20, 30, 40] * 2 would repeat the list. With an array, the multiplication happens to each element. That one difference is the heart of NumPy.

Memory: lists versus arrays

You can measure the memory difference directly. sys.getsizeof reports the size of one object, and for a list we must add the objects it points to.

Python Memory used by a list and an array

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The array needs exactly n * itemsize bytes of data. The list needs several times more, because every integer is a separate object.

Speed: a loop against a vectorized call

Here is the classic comparison. We square one million numbers, first with a Python loop over a list, then with a single NumPy expression. Timings differ from machine to machine (and in the browser), so the example prints only the verdict, not the seconds.

Python Python loop versus NumPy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

On a typical computer the NumPy version is 10 to 100 times faster for this kind of task. The bigger the arrays, the bigger the gap, because the fixed cost of calling into C is spread over more elements. Writing Fast NumPy Code later shows how to measure this carefully.

Note: NumPy is not faster at everything. For a handful of values, or for code full of if branches per element, a plain list can win. NumPy shines when you apply the same operation to a lot of numbers.

NumPy 2 and versions

This tutorial teaches NumPy 2.x. The 2.0 release (2024) cleaned up the API: many old aliases such as np.float_ and np.bool8 were removed, the type promotion rules changed (see Converting and Casting Types), and the random generator API became the recommended way to draw random numbers. Code written in this tutorial uses the modern names, and it still works in recent 1.x versions in most cases.

You can check which version you have with np.__version__, but the examples on this site never print it because the number changes with every release.

Where NumPy fits in the Python data stack

NumPy is rarely used alone. It is the foundation that most other scientific libraries are built on:

graph TD
    NP["NumPy<br/>ndarray + fast math"]
    NP --> PD["pandas<br/>tables and time series"]
    NP --> SP["SciPy<br/>optimization, statistics, signals"]
    NP --> MP["Matplotlib<br/>plots"]
    NP --> SK["scikit-learn<br/>machine learning"]

Most of the Python data stack passes data around as NumPy arrays.

  • pandas wraps arrays in labeled tables (DataFrames). Each column is an array underneath.
  • SciPy adds scientific algorithms: optimization, special functions, sparse matrices, statistics.
  • Matplotlib draws plots from arrays of numbers.
  • scikit-learn and most deep-learning libraries take arrays as input and return arrays.

Learning NumPy well makes all of these easier, because they share its vocabulary: shapes, dtypes, broadcasting and axes.

What you will learn

This tutorial moves from the ground up. You will create arrays, understand data types, select elements with slices and masks, do math across whole arrays, reshape and combine data, use linear algebra and random numbers, work with real data such as images and signals, tune performance, and finish with projects like Conway's Game of Life and a tiny neural network.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does np.array([1, 2, 3]) * 2 produce?

    Arithmetic on an ndarray is elementwise, so every value is multiplied by 2. Repeating a sequence is the behavior of a plain Python list.
  2. 2. Why does an ndarray use less memory than a list of the same numbers?

    All elements share one dtype, so NumPy can store the plain values in one contiguous block. A list stores a pointer per item, plus a separate Python object for each value.
  3. 3. Which import line is the universal convention?

    np is the shared alias across tutorials, books and projects. Star imports are discouraged because they shadow built-ins such as sum and max.
  4. 4. Which library stores the columns of its tables as NumPy arrays and adds labels on top?

    pandas builds labeled DataFrames on top of NumPy arrays. The others are unrelated parts of the standard library.

Summary

  • NumPy's ndarray is a compact block of same-typed values with fast, compiled math.
  • Arrays use far less memory than lists of numbers and avoid slow Python loops.
  • Import with import numpy as np; this tutorial uses NumPy 2 names and behavior.
  • pandas, SciPy, Matplotlib and scikit-learn are all built on NumPy arrays.
  • NumPy wins on large amounts of numeric data, not on tiny or branch-heavy tasks.

Next: Your First Arrays