Skip to content

       

Welcome! Let's get to know NumPy.

  • NumPy is the core library for scientific computing in Python.
  • Foundational Python libraries are built on top of NumPy's API.

Importing the NumPy package

# Import NumPy
import numpy as np

NumPy's main object: arrays

NumPy arrays store data in any number of dimensions. Here's a look at one, two, and three-dimensional arrays:

NumPy arrays can be created from:

  • Python lists
  • .csv files
  • NumPy functions such as np.arange()
  • ...and more!

Creating arrays from lists

We can create an array from a Python list using the np.array() function.

To make a one-dimensional array, pass the list as an argument to np.array():

new_array = np.array(list_name)

To make a two-dimensional array, pass a tuple of lists as an argument to np.array():

new_array = np.array((list_name_1, list_name_2))

# Let's start with a few lists
list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
# Create a 1-dimensional array from list_1
array_from_list_1 = np.array(list_1)
array_from_list_1
# Create a 2-dimensional array from list_1 and list_2
array_from_list_1_and_2 = ...
array_from_list_1_and_2