NumPy Data Types

dtype and type conversion.

✍️ 3 exercises + code challenge in this lesson

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)

Key Takeaways: NumPy Data Types

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.

NumPy Data Types Code Example

NumPy
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))
Output
float64
[1 2 3] int64
[ True False  True]
Free sign-in required · Google login available

NumPy Data Types Practice Exercises

Check what you learned in “NumPy Data Types”.

/ 3 solved
Fill in the blank

Convert the array to integers.

ints = arr.(int)
✓ Correct! Not quite. Try again.
Multiple choice

Can one NumPy array hold mixed data types like a list?

✓ Correct! Not quite. Try again.
Code challenge

Convert [1.9, 2.5, 3.1] to integers and print the result as a list with .tolist().

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

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