← Back to NumPy Course | Chapter 1: Getting Started | Lesson 5 of 7

ndarray basics

The ndarray is NumPy's grid of numbers; it can be a line, a table, or even a cube of values.

In this page:

  1. ndarray basics
Syntax
python
import numpy as np
arr = np.array([value1, value2, value3])
arr.shape
arr.dtype

ndarray basics

The ndarray is a homogeneous, fixed-size, N-dimensional container. A 1-D array is like a vector, a 2-D array is like a matrix, and higher dimensions work the same way. Every array knows its own shape and data type.

Note: Nested lists of equal length become a 2-D array; ragged lists do not.

Example: ndarray basics

python
import numpy as np

v = np.array([1, 2, 3])
m = np.array([[1, 2, 3], [4, 5, 6]])

print("Vector:", v, "shape", v.shape)
print("Matrix:")
print(m)
print("Matrix shape:", m.shape)

# Output:
# Vector: [1 2 3] shape (3,)
# Matrix:
# [[1 2 3]
#  [4 5 6]]
# Matrix shape: (2, 3)
Related Topics
Common Mistakes
  1. Passing ragged nested lists and expecting a clean 2-D array
  2. Confusing the array itself with a Python list
  3. Forgetting that NumPy indexes from 0
Chapter Summary
  • ndarray is homogeneous and N-dimensional
  • Nested lists build multi-dimensional arrays
  • Arrays expose .shape and .dtype
  • Indexing starts at 0
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.