Skip to main content
Albero Academy

Data Types (dtype)

Why every array has a single dtype, how to choose it, and how type promotion works.

Foundations9 min

Every array has a single dtype — the type of every element. It controls how much memory the array uses and how it behaves in arithmetic. Picking the right dtype keeps data correct and storage small.

Common dtypes

dtypeUse for
int64 / int32Whole numbers
float64 / float32Real numbers (the default for maths)
boolTrue/False masks
complex128Complex numbers
uint8Bytes / image pixels (0–255)
setting and converting dtype
a = np.array([1, 2, 3], dtype=np.float64)
a.dtype # dtype('float64')
a.astype(np.int32) # convert (returns a new array)

Type promotion

When you combine arrays of different types, NumPy promotes to a type that fits both — the same rule that turned an int array float when you added a float:

promotion in action
np.array([1, 2, 3]) + 0.5 # float64 result
(np.array([1, 2]) / 2).dtype # float64 — true division always floats

Watch for overflow

Key takeaways

  1. 01Every array has one dtype controlling memory use and arithmetic behaviour.
  2. 02Set it with dtype=… and convert with .astype() (returns a new array).
  3. 03Combining types promotes to a common type; true division (/) always yields floats.
  4. 04Fixed-width integers can overflow silently — mind uint8 for image data.

Finished this chapter?

Mark it complete to track your progress through NumPy.