Lessons · Coding · beginner · freeCodeCamp.org

Python NumPy Tutorial for Beginners

The video “Python NumPy Tutorial for Beginners” by freeCodeCamp.org as 9 lessons, with the idea explained, the code from the screen, steps to try in the practice terminal, practice problems with solutions and a quiz that leads back to the moment.

9lessons
14pieces of code
1formula
22exercises
58min of video
Open the lessons →Lesson 1 is free · Beyond Pass opens all
Python NumPy Tutorial for Beginners, video by freeCodeCamp.org▶ 58:09 · freeCodeCamp.org
123456789
0:009 lessons across the whole video58:09

You will be able to

  • Create NumPy arrays of one, two, and three dimensions from Python lists
  • Inspect array shape, dimensions, dtype, and memory size using attributes like shape, ndim, dtype, and itemsize
  • Index, slice, and modify elements, rows, and columns using row-column notation and step sizes
  • Generate arrays with zeros, ones, full, random values, identity matrices, and repetition
  • Apply element-wise arithmetic, matrix multiplication, reshaping, stacking, and statistics to arrays
  • Load data from text files and filter arrays with boolean masks and conditions

Good to know first

  • Basic Python syntax, including lists and list indexing
  • Installing packages with pip from a terminal
  • Basic familiarity with what a matrix or table of numbers is

Syllabus

9 lessons and a final project

  1. 1

    Why NumPy is faster than lists: fixed types and contiguous memory Free

    0:00–8:178 min of video

    This lesson explains the two main reasons NumPy arrays beat Python lists for speed: every item has a fixed type stored in fewer bytes, and the items sit side by side in memory. You will see how a single number like 5 is stored in each case and why that makes NumPy faster.

    3 exercises3 questions
  2. 2

    Where NumPy is used, creating arrays, and checking shape and dtype Beyond Pass

    8:17–16:098 min of video

    You will see why NumPy arrays do item-by-item maths that plain lists cannot, and how NumPy is used in plotting, pandas, images and machine learning. You will also create 1D and 2D arrays and check their dimension, shape, data type and memory size.

    1 code7 build steps2 exercises3 questions
  3. 3

    Indexing, Slicing and Assigning Values in 2D and 3D Arrays Beyond Pass

    16:09–23:057 min of video

    You will learn how to pick out single elements, whole rows, columns and ranges from a NumPy array using row, column and slice notation. You will also learn how to change those values and how to index a 3D array by working from the outside in.

    3 code6 build steps3 exercises4 questions
  4. 4

    Creating Arrays: zeros, ones, full, random, identity and repeat Beyond Pass

    23:05–31:348 min of video

    You will learn how NumPy builds ready-made arrays in one line: filled with zeros, ones or any value, filled with random numbers, as an identity matrix, or by repeating an existing array. You will also learn the small shape-syntax differences that trip beginners up.

    2 code7 build steps2 exercises3 questions
  5. 5

    Build an Array from Pieces, and Copy vs Reference in NumPy Beyond Pass

    31:34–35:494 min of video

    You will build a 5x5 array with a 3x3 block of zeros and a 9 in the middle by combining np.ones, np.zeros, indexing and slicing. You will also learn why b = a does not make a real copy, and how .copy() fixes it.

    3 code5 build steps3 exercises4 questions
  6. 6

    Element-wise maths, universal functions and linear algebra with NumPy Beyond Pass

    35:49–42:197 min of video

    You will learn how NumPy applies arithmetic to every element of an array at once, and how it differs from matrix multiplication. You will also see how to apply functions such as sine, check a matrix's determinant, and why the sizes of matrices must match before you multiply them.

    1 code1 formula4 build steps3 exercises3 questions
  7. 7

    Statistics, reshaping and stacking NumPy arrays Beyond Pass

    42:19–47:325 min of video

    You will learn how to get min, max and sum of a NumPy array overall or along rows and columns, how to reshape an array into a new shape with the same number of values, and how to stack arrays vertically and horizontally. Each idea is shown with the exact calls the video uses.

    1 code6 build steps2 exercises3 questions
  8. 8

    Loading Text Files and Filtering with Boolean Masks Beyond Pass

    47:32–56:008 min of video

    You will load comma-separated data from a text file into a NumPy array with genfromtxt, change its type with astype, and pick out values with boolean masks. You will also combine conditions and use any and all along an axis.

    2 code5 build steps2 exercises4 questions
  9. 9

    Indexing quiz: rows, columns and lists together Beyond Pass

    56:00–58:092 min of video

    This quiz checks three NumPy indexing patterns: slices for a block of rows and columns, lists of indices to pick scattered rows and columns, and mixing a list with a slice. You will work each one out before seeing the answer.

    1 code3 build steps2 exercises3 questions

Final project

Class Test Scores Analyser

Build a small NumPy program that creates, loads and analyses a table of test scores for several students across several subjects. It uses the whole course: array creation, inspection, indexing, copying, maths, statistics, reshaping, stacking and boolean masks.

Its 7 tasks open with Beyond Pass.

Free preview · Lesson 1 in full

Why NumPy is faster than lists: fixed types and contiguous memory

This lesson explains the two main reasons NumPy arrays beat Python lists for speed: every item has a fixed type stored in fewer bytes, and the items sit side by side in memory. You will see how a single number like 5 is stored in each case and why that makes NumPy faster.

0:00 to 8:17 in the video

Scratchpad

Draw and write next to the video.

The idea

NumPy is a library for multi-dimensional arrays. You can store data in one-dimensional arrays, two-dimensional arrays (tables with rows and columns), three-dimensional arrays and more. It is the base that many data science libraries, such as pandas, are built on. A common question is: why use NumPy instead of Python lists? The main answer is speed, and the video explains two reasons for it.

The first reason is fixed types. Computers do not see the digit 5; they see binary, and the number 5 is 00000101 in binary, which is one byte (8 bits). By default NumPy stores this value as an int32, which uses four bytes, with the same 5 written in all 32 bits. You can choose a smaller type, such as int16 (two bytes) or int8 (one byte), when your values are small. A Python list is different. Each integer in a list is a full Python object, with an object value, an object type, a reference count and a size. That is far more memory for one number. Less memory means the computer has fewer bytes to read, so it is quicker. Also, because every NumPy item has the same type, the computer does not need to check the type of each item while it loops through the array. A list can mix an integer, a float, a string and a boolean, so each element's type must be checked.

The second reason is contiguous memory. Think of computer memory as a row of numbered blocks. A Python list is a set of pointers: the list holds the addresses of its items, and those items can be scattered all over memory with gaps between them. To process the whole list, the computer has to jump around memory. A NumPy array stores all its items in one unbroken run of blocks, right next to each other.

Contiguous memory brings two benefits. First, the CPU has SIMD (single instruction, multiple data) vector units. When data sits side by side, one instruction can work on many values at once, for example adding a whole block of numbers in one go. Second, the cache is used better. The cache is a small, fast memory close to the CPU. When data is packed together, a load brings many useful values into the cache at once. With a scattered list, the computer must make slower lookups to reach each value.

In short: NumPy is faster because it reads fewer bytes (fixed, small types), skips type checks while looping, and keeps its data in one contiguous block that the CPU can process in parallel and cache efficiently. You do not need to manage this yourself; you get the speed just by using NumPy arrays.

Key points

  • A NumPy array stores all items with one fixed type, so there is no type checking while looping. 3:00
  • The default NumPy integer is int32, which uses 4 bytes; a Python int in a list is a bigger object with extra fields. 4:09
  • Less memory to read means the computer does less work, so NumPy is faster. 5:00
  • A list holds pointers to items scattered in memory; a NumPy array stores items side by side in contiguous memory. 5:30
  • Contiguous memory allows SIMD vector processing and more effective cache use. 8:00

Words to know

Byte
A group of 8 bits, the unit computers use to read and store data.
int32
A fixed 32-bit (4-byte) integer type; NumPy's default integer.
Fixed type
All items in a NumPy array share one data type, so the computer knows the size and type without checking each item.
Contiguous memory
Data stored in one unbroken block of memory, with items next to each other.
SIMD
Single instruction, multiple data: one CPU instruction works on many values at once.
Cache
A small, fast memory close to the CPU that keeps recently used or nearby data.

Worked example

How many bytes does the number 5 take up as an int32 in NumPy, and how many bits is that?

  1. The number 5 is written in binary as 00000101, which is 8 bits (one byte).
  2. NumPy's default int32 type uses 32 bits for each value.
  3. 32 bits divided by 8 bits per byte gives 4 bytes.
4 bytes (32 bits), even though 5 only needs one byte.

Watch out

  • Do not confuse the byte size of a value with its meaning: the number 5 is the same in all cases, but NumPy spends 4 bytes on it by default while a small type like int8 uses only 1 byte. 3:00
  • Speed is not just about the number of items: the layout of memory and the fixed type matter as much as the count. 5:30
  • You can only get these benefits when all items share a type; a mixed list gives up the fixed-type advantage. 5:00

Practice

Calculate Solution checked

A NumPy array holds 1000 values, each stored as int32 (4 bytes). How many bytes of memory does the array's data take up?

Hint

Multiply the number of values by the bytes per value.

Solution
  1. Number of values = 1000.
  2. Bytes per value for int32 = 4.
  3. Total = 1000 × 4 = 4000 bytes.

4000

Calculate Solution checked

The same 1000 values are stored using int8 (1 byte each) instead of int32. How many bytes are needed now?

Hint

int8 uses one byte per value.

Solution
  1. Number of values = 1000.
  2. Bytes per value for int8 = 1.
  3. Total = 1000 × 1 = 1000 bytes.

1000

Explain Solution checked

In your own words, explain why a list of mixed items (an integer, a string, and a float) is slower to loop through than a NumPy array of integers.

Hint

Think about the type of each item and what the computer must check.

Solution
  1. In a mixed list, each item can have a different type, so the computer must check the type of each one as it loops.
  2. In a NumPy array every item has the same fixed type, so no type checks are needed and the computer knows how many bytes each item uses.

A model answer: The list items can be different types, so Python must check each item's type while looping, which costs time. A NumPy array has one fixed type for all items, so there are no checks and the memory layout is predictable, which makes it faster.

On your scratchpad

Draw two rows of boxes. In the top row, draw 8 boxes with gaps between some of them and arrows from a small list to each box (a list of pointers). In the bottom row, draw 8 boxes packed side by side (a NumPy array). Label each row.

Quiz

  1. Why does NumPy read data faster than a Python list?

    1. It stores items with a fixed type in contiguous memory
    2. It uses a faster CPU than Python
    3. It stores each number as text
    4. It deletes items it does not need
    Show the answer

    A. It stores items with a fixed type in contiguous memory · Fixed types make items small and predictable, and contiguous memory lets the CPU read and process them efficiently.

    5:00 watch the moment
  2. By default, how many bytes does NumPy use for an integer such as 5?

    1. 1 byte
    2. 2 bytes
    3. 4 bytes
    4. 8 bytes
    Show the answer

    C. 4 bytes · NumPy's default integer is int32, which uses four bytes.

    4:09 watch the moment
  3. What does SIMD stand for, and what does it allow?

    1. Simple Input Memory Device; it saves disk space
    2. Single instruction, multiple data; one instruction works on many values at once
    3. Sorted integer multiple data; it sorts arrays
    4. Shared index memory data; it links lists together
    Show the answer

    B. Single instruction, multiple data; one instruction works on many values at once · SIMD lets the CPU perform the same operation on many contiguous values in one go.

    8:00 watch the moment

Open lesson 1, with the video →

Lessons 2–9 and the final project

They open with Beyond Pass: every lesson of every course, in full. One payment: $14.99 for 90 days or $29 for a year. No subscription.

Made by Beyond from the video “Python NumPy Tutorial for Beginners” by freeCodeCamp.org. The video plays in YouTube's own player. Watch it on YouTube · All lessons