Creating 1D and 2D Arrays
In NumPy, arrays are the main way to store and work with data.
You can create them using the np.array()
function.
Let’s look at two types: 1D arrays and 2D arrays.
What Is Jupyter Notebook?
Jupyter Notebook is an interactive coding tool.
It lets you write and run Python code in small blocks (called "cells") and see the output right away.
We’ll use Jupyter Notebooks throughout this course to test NumPy code and practice working with arrays.
1D Array
A 1D array is a single row of numbers.
You create it from a simple list.
import numpy as np
arr1d = np.array([10, 20, 30])
print(arr1d) # [10 20 30]
print(arr1d.shape) # (3,)
print(arr1d.ndim) # 1
print(arr1d.size) # 3
.shape
tells you the number of elements (3,).ndim
shows it's 1-dimensional.size
gives the total number of items
2D Array
A 2D array is like a grid with rows and columns. You pass in a list of lists.
arr2d = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(arr2d)
# [[1 2 3]
# [4 5 6]]
print(arr2d.shape) # (2, 3)
print(arr2d.ndim) # 2
print(arr2d.size) # 6
This array has:
- 2 rows and 3 columns
- A shape of
(2, 3)
- 6 total elements
Summary
Use np.array()
to create arrays from lists.
- A single list → 1D array
- A list of lists → 2D array
You can inspect the array using:
.shape
: dimensions (rows, columns).ndim
: how many dimensions.size
: total number of elements
What’s Next
You’ll now practice creating arrays using real code in a Jupyter Notebook.