Array Data Types (dtypes)

A dtype is the data type that describes what kind of value every element of a NumPy array holds and how much memory it uses — because the ndarray is homogeneous, that single dtype applies to the entire array at once.

Learn Array Data Types (dtypes) in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll meet the common dtypes, learn to set one at creation, convert between them with .astype(), and avoid the overflow and precision traps that catch beginners.

A dtype tells NumPy what each element is and how many bytes it occupies. Common ones include int64 (whole numbers), float64 (decimals), bool (True/False), and complex128 (complex numbers). Read it with the .dtype attribute.

Expected output: int64 , float64 , and bool — NumPy infers the dtype from the values you supply.

You don't have to accept the inferred type. Pass dtype= to any creation function to force a specific type. This is useful for saving memory (a smaller int) or guaranteeing decimals (a float).

Expected output: the float array [1. 2. 3.] with dtype float64 , and an int32 zeros array.

To change an existing array's type, call .astype() . It returns a brand-new array; the original is untouched. Converting floats to ints truncates toward zero (it drops the decimal part, it does not round).

Expected output: [10 20 30] (decimals truncated), [10. 20. 30.] , and the original [10.9 20.2 30.7] still intact.

Small integer dtypes save memory but have limits. An int8 only holds -128 to 127, so going past the limit wraps around . You can also check how much space an array uses with .itemsize (bytes per element) and .nbytes (total bytes).

Expected output: [-128] from the overflow, then 8 bytes per int64 element and 24 bytes total.

Replace each ___ so the program creates a float array and converts it to integers.

Expected output: [1. 2. 3.] , [1 2 3] , then int32 . (Answers: float64 , astype , dtype .)

A narrow dtype like int8 overflowed past its limit and wrapped around.

✅ Fix: create the array with a wider dtype such as np.int64 .

Casting a float to an int truncates toward zero — it does not round.

✅ Fix: round first with np.round(arr).astype(np.int64) if you need rounding.

Make a float64 array, convert it to int16, and compare how many bytes each uses.

Lesson 4 complete — you control your dtypes!

You now know the common dtypes, how to set one at creation, how to convert with .astype(), and how to avoid overflow while keeping an eye on memory with itemsize and nbytes.

🚀 Up next: Array Attributes — read shape, ndim, and size to understand any array's structure at a glance.

Practice quiz

What is a dtype in NumPy?

  • The shape of the array
  • The data type describing each element and its byte size
  • The number of dimensions
  • The array's variable name

Answer: The data type describing each element and its byte size. A dtype describes what each element is (int64, float64, bool, ...) and how many bytes it uses.

What dtype does np.array([1.0, 2.0]) get by default?

  • int64
  • float32
  • bool
  • float64

Answer: float64. Decimal literals make NumPy infer float64.

How do you force a specific dtype when creating an array?

  • Pass the dtype= keyword
  • Call .shape()
  • Use np.cast
  • It cannot be set at creation

Answer: Pass the dtype= keyword. Every creation function accepts dtype=, e.g. np.zeros(4, dtype=np.int32).

Which method converts an existing array to a new dtype?

  • .dtype()
  • .reshape()
  • .astype()
  • .convert()

Answer: .astype(). .astype(newtype) returns a new array cast to the requested dtype.

What does np.array([1.9, 2.1]).astype(np.int32) return?

Float-to-int truncates toward zero (drops the decimals), giving [1, 2].

What does np.array([127], dtype=np.int8) + 1 return?

int8 maxes at 127, so adding 1 overflows and wraps to -128.

What is the itemsize of an int64 array element?

  • 8
  • 4
  • 2
  • 16

Answer: 8. int64 uses 64 bits = 8 bytes per element.

What does .nbytes report for np.array([1, 2, 3], dtype=np.int64)?

  • 3
  • 8
  • 24
  • 64

Answer: 24. 3 elements * 8 bytes each = 24 bytes total.

Why might a large integer suddenly turn negative?

  • NumPy rounds randomly
  • Integer overflow wrapped a narrow dtype past its limit
  • astype always negates
  • The array became a float

Answer: Integer overflow wrapped a narrow dtype past its limit. A fixed-width int dtype like int8 wraps around when a value exceeds its range.

How can you avoid integer overflow for large values?

  • Use a narrower dtype like int8
  • Use a wider dtype like int64
  • Convert to bool
  • Reduce the array size

Answer: Use a wider dtype like int64. A wider dtype such as int64 holds a much larger range of values.