Lesson 1 of 23
Nextโฏ

PY-401 ยท NumPy foundations

NumPy arrays

โฑ 9 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

1of 23

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    A NumPy array is a grid of values that all have the same type, stored side by side in memory so maths on millions of them runs fast. You make one with np.array([...]), and every array knows its shape (rows and columns), ndim (number of dimensions) and dtype (the type of every value). It is the building block under pandas, matplotlib and almost every Python AI library.

    • Create an array from a list
    • Read shape, ndim and dtype
    • Make arrays with zeros, arange and linspace

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    NumPy arrays, running
    1. import numpy as np
    2. prices = np.array([120.0, 80.5, 99.9, 45.0])
    3. print(prices)
    4. print(prices.dtype, prices.shape, prices.ndim)
    5. print(np.zeros((2, 3)))
    6. print(np.arange(0, 10, 2))
    7. print(np.linspace(0, 1, 5))
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemA shop has 2 million sales rows. Adding 18% tax to each price with a Python for loop takes seconds. With a NumPy array it is one line and finishes before you blink.
    2. The Python ideaimport numpy as npA NumPy array is a grid of values that all have the same type, stored side by side in memory so maths on millions of them runs fast.
    3. In real lifeA fitness app stores a year of step counts as one array of 365 numbers. The weekly chart, the monthly average and the "best day" badge all come from that one array.

    Start with a question

    A shop has 2 million sales rows. Adding 18% tax to each price with a Python for loop takes seconds. With a NumPy array it is one line and finishes before you blink.

    Data science is maths on many numbers at once. NumPy does that maths in fast compiled code, and pandas tables are NumPy arrays underneath.

    Explain it like I'm 10

    A Python list is a bag where you can throw in anything: a toy, a sock, a number. A NumPy array is an egg box: every slot is the same size and holds the same kind of thing, so you can check all the eggs at once.

    The technical explanation

    Start with import numpy as np. Everyone uses the short name np, so every example you find online matches yours.

    np.array([120.0, 80.5]) copies a list into an array. A list of lists becomes a 2-D array: rows and columns.

    .shape is a tuple: (4,) is 4 values in a row, (2, 3) is 2 rows of 3. .ndim counts the dimensions. .dtype is the type, such as float64 or int64.

    np.zeros((2, 3)) makes a 2 by 3 grid of 0.0, np.arange(0, 10, 2) works like range but returns an array, and np.linspace(0, 1, 5) gives 5 evenly spaced numbers from 0 to 1, end included.

    Python listNumPy array
    Types insideAnything, mixedOne type for all
    Maths on all itemsNeeds a loopOne expression
    Speed on a million numbersSlowFast (compiled code)
    Size can changeYes, appendNo, make a new array

    Syntax

    import numpy as np
    a = np.array([1.5, 2.0, 3.5])
    a.shape, a.ndim, a.dtype
    np.zeros((rows, cols))
    np.arange(start, stop, step)
    np.linspace(start, stop, count)

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    A fitness app stores a year of step counts as one array of 365 numbers. The weekly chart, the monthly average and the "best day" badge all come from that one array.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. How does almost everyone import NumPy?
    2. 2. a = np.zeros((3, 4)). What is a.shape?
    3. 3. What is special about the values in one NumPy array?
    4. 4. What does np.arange(0, 6, 2) contain?
    5. 5. np.linspace(0, 10, 3) givesโ€ฆ

    Questions people ask

    What is the difference between a Python list and a NumPy array?

    A list can hold mixed types and grow with append. A NumPy array holds one type in a fixed-size block of memory, so maths on every value runs in fast compiled code without a Python loop.

    Why is NumPy faster than a Python loop?

    The loop over the values runs inside NumPy's compiled C code instead of the Python interpreter, and the numbers sit next to each other in memory, so the CPU can process them in bulk.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner What is the difference between a Python list and a NumPy array?

    A list can hold mixed types and grow with append. A NumPy array holds one type in a fixed-size block of memory, so maths on every value runs in fast compiled code without a Python loop.

    beginner Why is NumPy faster than a Python loop?

    The loop over the values runs inside NumPy's compiled C code instead of the Python interpreter, and the numbers sit next to each other in memory, so the CPU can process them in bulk.

    intermediate How does almost everyone import NumPy?

    import numpy as np. np is the shared convention, so examples everywhere look the same.

    intermediate a = np.zeros((3, 4)). What is a.shape?

    (3, 4). shape is (rows, columns): 3 rows of 4.

    Quick revision

    Remember

    • Create an array from a list
    • Read shape, ndim and dtype
    • Make arrays with zeros, arange and linspace
    • Every value in an array has the same type. np.array([1, 2, "3"]) quietly turns all three into text. Check .dtype when numbers come from a file.

    Syntax

    import numpy as np
    a = np.array([1.5, 2.0, 3.5])
    a.shape, a.ndim, a.dtype
    np.zeros((rows, cols))
    np.arange(start, stop, step)
    np.linspace(start, stop, count)

    Official documentation