Last updated

Data Types (dtype)

Definition: Every NumPy array has a single data type, stored in its dtype. All elements share that type — this is what makes arrays so fast and compact.

Example 1 — checking the type

import numpy as np
a = np.array([1, 2, 3])
print(a.dtype)      # int64

b = np.array([1.0, 2.5, 3.0])
print(b.dtype)      # float64

Example 2 — converting with astype

import numpy as np
a = np.array([1, 2, 3])
c = a.astype(float)
print(c)            # [1. 2. 3.]
print(c.dtype)      # float64

d = np.array([1.9, 2.9]).astype(int)
print(d)            # [1 2]  (decimals are truncated)

Why it matters

Mixing types forces NumPy to pick one for the whole array. np.array([1, 2.5]) becomes all floats. Knowing the dtype helps you avoid surprises in calculations.

💡 Tip: converting float to int truncates (chops off decimals), it does not round.

Try it Yourself
Output

          
Ad · responsive