No history yet

NumPy Basics

Why Use NumPy?

Python's built-in lists are flexible. You can store different data types in a single list, like numbers and text. But this flexibility comes at a cost: performance. When you're working with large amounts of numerical data, especially for tasks in data science or machine learning, Python lists can be slow.

Think of a Python list as a toolbox full of random items. You might have a hammer, a screwdriver, and a roll of tape. To find what you need, you have to rummage through everything. A NumPy array, on the other hand, is like a perfectly organized tray of sockets, all the same size and type, laid out in a predictable grid. Accessing any specific one is instant.

This is because NumPy arrays store data in a continuous block of memory. All elements are the same data type, which allows NumPy to perform mathematical operations on the entire array at once, much faster than looping through a Python list element by element.

NumPy (Numerical Python) is the foundational package for numerical computing in Python.

This efficiency is why NumPy is a cornerstone of the scientific Python ecosystem. Many other popular libraries, like pandas and scikit-learn, are built on top of it.

Getting Started

Before you can use NumPy, you need to install it. If you have Python and its package manager, pip, installed, you can install NumPy by running a simple command in your terminal or command prompt.

pip install numpy

Once installed, you need to import it into your Python script. The standard convention is to import it with the alias np. This saves you from typing numpy every time you call one of its functions.

import numpy as np

Now you're ready to create your first array. The most common way is to convert a Python list into a NumPy array using the np.array() function.

# Create a Python list
my_list = [1, 2, 3, 4, 5]

# Convert the list to a NumPy array
my_array = np.array(my_list)

print(my_array)
# Output: [1 2 3 4 5]

The ndarray

When you create an array with NumPy, you are creating an object called an ndarray, which stands for "N-dimensional array."

ndarray

noun

The core data structure in NumPy. It is a multidimensional, fixed-size array of items of the same type.

Every ndarray has important attributes that describe it. For example, .shape tells you the dimensions of the array, and .dtype tells you the data type of its elements.

a = np.array([1, 2, 3])
print(a.shape)
# Output: (3,)

print(a.dtype)
# Output: int64

b = np.array([[1, 2, 3], [4, 5, 6]])
print(b.shape)
# Output: (2, 3)

In the examples above, a is a 1-dimensional array with 3 elements. b is a 2-dimensional array with 2 rows and 3 columns.

Understanding the ndarray and its properties is the first step to unlocking NumPy's power for fast and efficient numerical computation.

Quiz Questions 1/5

What is the primary advantage of using a NumPy array over a standard Python list for large-scale numerical computations?

Quiz Questions 2/5

What is the standard convention for importing the NumPy library to use in a Python script?

Now you have the basics down. You know what NumPy is, why it's faster than standard Python lists for numerical work, and how to create the fundamental ndarray object.