Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 49 ยท Data Types

Data Types (dtype)

Learn NumPy data types: int8 to int64, unsigned ints, float16 to float64, bool, complex and strings. Choose a dtype and avoid silent integer overflow.

Every NumPy array has exactly one dtype, the data type shared by all of its elements. The dtype decides how many bytes each element takes, what values it can hold, and how arithmetic behaves. Picking it well saves memory; picking it carelessly causes some of NumPy's most surprising bugs, such as numbers that quietly wrap around.

In this lesson you will tour the main families (integers, floats, booleans, complex numbers, strings), see the defaults NumPy chooses, measure memory per element, and run into the overflow pitfall on purpose so you can recognize it later.

The main dtype families

Family Names Notes
Signed integers int8, int16, int32, int64 Number is the bits per element
Unsigned integers uint8, uint16, uint32, uint64 No negatives, double the positive range
Floating point float16, float32, float64 Approximate decimals
Boolean bool One byte per element
Complex complex64, complex128 Real and imaginary parts
Text str (<U5...), StringDType Fixed width or variable length
Generic object Pointers to Python objects, slow

You can name a dtype with a class (np.int32), a string ("int32", "i4") or the Python type (int, float, bool). All of these work in dtype=.

Defaults

If you do not say otherwise, NumPy picks:

  • int64 for whole numbers on most 64-bit systems (the Python int maps to the platform's default integer),
  • float64 for numbers with a decimal point,
  • bool for True and False,
  • complex128 for complex numbers.
Python Default dtypes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line shows a text dtype such as <U3: a fixed-width Unicode string of at most 3 characters. The < marks byte order and U means Unicode. NumPy picked width 3 to fit the longest string.

Memory per element

The number in a dtype name is the number of bits; divide by eight for bytes. Smaller dtypes save memory, which matters for millions of values or for images.

Python Bytes per element and total size

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A million float64 values cost 8 MB, but the same count as uint8 costs only 1 MB. That is why photos are stored as uint8 arrays (values 0 to 255).

Value ranges

Integers have hard limits. np.iinfo and np.finfo report them; Converting and Casting Types goes deeper on floating-point limits.

Python Integer ranges with iinfo

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

An int8 holds -128 to 127 and a uint8 holds 0 to 255. With 8 bits you only have 256 patterns, whatever you decide they mean.

Choosing a dtype

graph TD
    Q["What are the values?"] --> W{"Whole numbers?"}
    W -- yes --> R{"Fit in 0..255?"}
    R -- yes --> U8["uint8"]
    R -- no --> I64["int64 (default)<br/>or int32 to save memory"]
    W -- no --> P{"Need high precision?"}
    P -- yes --> F64["float64 (default)"]
    P -- no --> F32["float32 for big data"]

A quick guide: keep the defaults unless memory or file format tells you otherwise.

Practical advice:

  • Keep the defaults (int64, float64) for ordinary computing. They are safe and fast enough.
  • Use float32 for very large datasets or machine learning, where halving memory matters more than the extra precision.
  • Use uint8 for images and bool for masks.
  • Avoid float16 for calculations; it has about three decimal digits of precision and is mainly a storage format.
  • Avoid object arrays unless you really need mixed Python objects: they lose NumPy's speed.

The overflow wraparound pitfall

Integer arrays do not raise an error when a result does not fit. They silently wrap around, like a car odometer rolling over.

Python Silent wraparound

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The results above are wrong for a reason that is easy to miss: no warning appears because NumPy treats array integer overflow as the normal behavior of the dtype. (Overflow of a single NumPy scalar does raise a RuntimeWarning, but arrays stay silent.) Defend yourself by choosing a wide enough dtype before the arithmetic, not after.

Warning: If an image stored as uint8 is brightened with img + 50, bright pixels wrap to dark ones. Convert to a wider type, add, then use np.clip before converting back.

Floats, complex and strings in action

Floats store approximations: float32 keeps about 7 significant decimal digits, float64 about 15 to 16. Complex numbers use Python's j notation. Strings can be fixed width or variable length with NumPy 2's StringDType.

Python Floats, complex numbers and strings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Fixed-width strings like <U5 silently truncate if you assign a longer value later, which is one reason StringDType is attractive. Structured Arrays and Text Data covers text in depth.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How many bytes does one float32 element use?

    The number in the name is bits: 32 bits is 4 bytes.
  2. 2. What is np.array([250, 251], dtype=np.uint8) + 10?

    Integer array arithmetic wraps modulo 256 for uint8 without any warning: 260 becomes 4 and 261 becomes 5.
  3. 3. Which dtype is the natural choice for a grayscale photo with values 0 to 255?

    uint8 holds exactly 0 to 255 in one byte per pixel, one eighth of the memory of int64 or float64.
  4. 4. What dtype does np.array([1, 2, 3]) have on a typical 64-bit system?

    Whole numbers default to the platform integer, which is int64 on common 64-bit systems.
  5. 5. Which is the safe way to avoid overflow when adding to a uint8 array?

    Overflow happens during the operation, so widen the dtype first, compute, then clip and narrow if needed.

Summary

  • Every array has one dtype; the defaults are int64, float64, bool and complex128.
  • The number in a dtype name is bits per element; itemsize gives bytes.
  • np.iinfo and np.finfo report the limits of integer and float types.
  • Integer arrays wrap around silently on overflow; widen the dtype before computing.
  • Use uint8 for images, float32 for large numeric data, and avoid object arrays when possible.

Next: Converting and Casting Types