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
| dtype | Use for |
|---|---|
int64 / int32 | Whole numbers |
float64 / float32 | Real numbers (the default for maths) |
bool | True/False masks |
complex128 | Complex numbers |
uint8 | Bytes / image pixels (0–255) |
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:
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
- 01Every array has one dtype controlling memory use and arithmetic behaviour.
- 02Set it with dtype=… and convert with .astype() (returns a new array).
- 03Combining types promotes to a common type; true division (/) always yields floats.
- 04Fixed-width integers can overflow silently — mind uint8 for image data.
