Every array has a single data type, its dtype: int64, float64, bool, <U5 (strings)…
- Choose it at creation:
np.array([1, 2], dtype="float32") - Convert with
astype():arr.astype(int)
dtype and type conversion.
✍️ 3 exercises + code challenge in this lessonEvery array has a single data type, its dtype: int64, float64, bool, <U5 (strings)…
np.array([1, 2], dtype="float32")astype(): arr.astype(int)Mastering NumPy Data Types helps you write clean and reliable NumPy code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
import numpy as np
a = np.array([1.7, 2.2, 3.9])
print(a.dtype)
b = a.astype(int)
print(b, b.dtype)
print(np.array([1, 0, 3]).astype(bool))
float64 [1 2 3] int64 [ True False True]
Check what you learned in “NumPy Data Types”.
Convert the array to integers.
ints = arr.(int)
Can one NumPy array hold mixed data types like a list?
Convert [1.9, 2.5, 3.1] to integers and print the result as a list with .tolist().
Free sign-in required · Google login available
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services