Lecture

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.

We'll focus on two common types: 1D arrays and 2D arrays.


About Notebooks

CodeFriends uses Notebooks to run Python code step by step.

Notebooks let you write and run small blocks of code called cells and see the output right away.

We'll use them throughout this course to test NumPy code and practice working with arrays.


1D Array

A 1D array is just a single row of numbers.

You create it from a simple Python list.

Creating a 1D Array
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: number of elements (3,)
  • .ndim: 1 dimension
  • .size: total number of items

2D Array

A 2D array is like a grid with rows and columns.

You create it by passing in a list of lists.

Creating a 2D Array
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
  • Shape (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 always inspect an array with:

  • .shape: dimensions (rows, columns)
  • .ndim: number of dimensions
  • .size: total elements
Quiz
0 / 1

In NumPy, a 2D array is created from a single list.

True
False

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help