← Back to NumPy Course | Chapter 3: Array Attributes | Lesson 4 of 6

dtype

The dtype attribute reports what kind of number each element is.

In this page:

  1. dtype
Syntax
python
arr.dtype
arr.dtype.name

dtype

dtype is an object describing the element type, with properties like name, kind and itemsize. It is set on creation and changed with astype. Checking dtype early prevents many subtle bugs from integer division or overflow.

Note: Print arr.dtype.name to get a clean string like int64.

Example: dtype

python
import numpy as np

a = np.array([1, 2, 3])
print(a.dtype, a.dtype.name)
a[0] = 9.9
print("after assigning 9.9:", a)
print(a.astype(np.float64).dtype)

# Output:
# int64 int64
# after assigning 9.9: [9 2 3]
# float64
Related Topics
Common Mistakes
  1. Assuming dtype changes automatically when values change
  2. Forgetting int arrays truncate assigned floats
  3. Confusing dtype with the Python type
Chapter Summary
  • dtype describes element type
  • Values assigned to an int array are truncated
  • astype creates a converted copy
  • dtype.name gives a readable label
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.