0Pricing
Python Academy · Lesson

NumPy Arrays and dtypes

Create ndarrays, understand dtypes, and explore array properties.

NumPy Arrays and dtypes is a free Python Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is NumPy?

NumPy provides the ndarray — a fixed-type, contiguous-memory array that supports vectorised operations orders of magnitude faster than Python lists.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(arr)          # [1 2 3 4 5]
print(arr * 2)      # [ 2  4  6  8 10]   (no Python loop)

Creating Arrays

Common creation functions: np.array(), np.zeros(), np.ones(), np.arange(), np.linspace().

import numpy as np

print(np.zeros((2, 3)))     # 2x3 array of 0.0
print(np.ones(5))           # [1. 1. 1. 1. 1.]
print(np.arange(0, 10, 2))  # [0 2 4 6 8]
print(np.linspace(0, 1, 5)) # [0.   0.25 0.5  0.75 1.  ]

dtypes

Every NumPy array has a single dtype. Common dtypes: int32, int64, float32, float64, bool, complex128.

import numpy as np

a = np.array([1, 2, 3])           # int64 by default
b = np.array([1.0, 2.0])          # float64
c = np.array([1, 2], dtype=np.float32)

print(a.dtype)  # int64
print(c.dtype)  # float32

Array Shape and ndim

shape is a tuple of dimension sizes. ndim is the number of dimensions. size is the total element count.

import numpy as np

m = np.ones((3, 4))
print(m.shape)   # (3, 4)
print(m.ndim)    # 2
print(m.size)    # 12
print(m.dtype)   # float64

Reshaping

reshape() returns a view with a new shape (same data). flatten() returns a copy as a 1-D array.

import numpy as np

arr = np.arange(12)
mat = arr.reshape(3, 4)
print(mat)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]
print(mat.flatten())   # [ 0  1  2  3 ...]

Type Casting

Use arr.astype(dtype) to convert an array to a different type. This always returns a copy.

import numpy as np

arr = np.array([1, 2, 3])
float_arr = arr.astype(np.float64)
print(float_arr)   # [1. 2. 3.]
print(float_arr.dtype)   # float64

Random Arrays

Generate random arrays with np.random: uniform, normal, integers.

import numpy as np

rng = np.random.default_rng(seed=42)   # reproducible
print(rng.random(5))         # 5 uniform [0,1)
print(rng.standard_normal(3)) # 3 normal(0,1)
print(rng.integers(1, 10, 4)) # 4 ints [1,10)

Inspecting Array Memory

itemsize gives bytes per element; nbytes gives total bytes.

import numpy as np

arr = np.zeros(1_000_000, dtype=np.float64)
print(arr.itemsize)   # 8
print(arr.nbytes)     # 8_000_000 (8 MB)

small = arr.astype(np.float32)
print(small.nbytes)   # 4_000_000 (4 MB)

np.empty and np.full

np.empty allocates without initialising (fast). np.full fills with a constant value.

import numpy as np

e = np.empty((2,3))          # uninitialised — random garbage values
f = np.full((2,3), fill_value=7)
print(f)
# [[7 7 7]
#  [7 7 7]]

Copy vs View

Slicing a NumPy array returns a view (no copy). Modifying the view modifies the original. Use .copy() for an independent array.

import numpy as np

arr = np.array([1, 2, 3, 4])
view = arr[1:3]
view[0] = 99
print(arr)    # [ 1 99  3  4]  — original changed!

copy = arr[1:3].copy()
copy[0] = 0
print(arr)    # unchanged

Structured Arrays

Structured arrays store heterogeneous data (like a C struct) in a single ndarray with named fields.

import numpy as np

dt = np.dtype([('name', 'U10'), ('age', np.int32), ('score', np.float64)])
people = np.array([
    ('Alice', 30, 95.5),
    ('Bob',   25, 87.3),
], dtype=dt)
print(people['name'])   # ['Alice' 'Bob']

Quick Check

What does arr.reshape(3, 4) return?

Recap

NumPy's ndarray is a fixed-type, contiguous-memory array. Key attributes: shape, dtype, ndim, nbytes. Slicing returns views; use .copy() for independence. Choose the smallest sufficient dtype to save memory.

Frequently asked questions

Is the “NumPy Arrays and dtypes” lesson free?

Yes — the full text of “NumPy Arrays and dtypes” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “NumPy Arrays and dtypes”?

Create ndarrays, understand dtypes, and explore array properties. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “NumPy Arrays and dtypes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. NumPy Arrays and dtypes
  2. Array Operations and Broadcasting
  3. Indexing, Slicing, and Fancy Indexing
  4. Linear Algebra with NumPy
← Back to Python Academy