/Curriculum/BeginnerArray Basics

04. NumPy Data Types (dtype)Beginner

Explore int8 to int64, float32, float64, boolean, and type casting with astype().

12 mins

Concept Overview

NumPy supports high-precision fixed-size C data types such as int32, int64, float32, float64, and bool. Explicit dtypes optimize RAM usage and GPU throughput.

Hardware Mental Model

Using int64 for small numbers between 1 and 10 is like shipping a feather in a massive shipping container. Using int8 saves 8x memory bandwidth!

Key Concepts (1)Click snippet to load in editor

.astype() returns a new array cast into the requested type (e.g. float64 to int32).

ints = floats.astype(np.int32)

⚠ Common Mistakes & Pitfalls

Avoid these frequent beginner syntax and logic traps
❌ Incorrect:arr = np.array([1.9, 2.8]); arr.astype(int)
✓ Correct:arr = np.array([1.9, 2.8]); arr = arr.astype(np.int32)

.astype() does not mutate the array in place; it returns a new array.

Pro Tip: Always reassign the variable: `arr = arr.astype(...)`.

📌 Quick Revision

Core takeaway points from this topic
NumPy arrays are homogeneous: every element shares the exact same dtype.
`int8` (1 byte), `int32` (4 bytes), `int64` (8 bytes).
`float32` (standard in Deep Learning), `float64` (scientific default).
Use `.astype()` to cast between data types safely.
Editor: 04. NumPy Data Types (dtype)
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state

🎯 Try It Yourself: Practice Challenge

Hands-on Mode

Solve the objective below using NumPy vectorized syntax

Objective:Cast Floats to Integers

Create `arr = np.array([1.1, 2.9, 3.5])`, cast it to `np.int32` using `.astype()`, and print it.

Expected Output Target:[1 2 3]
Practice Workspace
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state