Before we start learning about NumPy arrays, let’s learn about the different data types within NumPy.
But even before we can do that, we must start by importing NumPy.
You can do this by typing the following:
import numpy as np
Usually, numpy is shortened to np so you don’t have to type out numpy when calling its functions.
Now that NumPy has been imported, we can start exploring the many data types NumPy has to offer.
NumPy supports far more data types than Python does normally.
NumPy has 5 basic numerical types: booleans (bool), integers (int), unsigned integers (uint) floating point (float) and complex.
You may sometimes see numbers behind these datatypes (e.g float32), these numbers indicate how many bits are required to represent that value in memory. So for example, float32 would require 32 bits of memory.
For the most part, NumPy datatypes (dtypes) are interchangeable with Python but the main benefit of using NumPy’s datatypes is better compatibility with NumPy arrays.
Below is an example of turning an integer into a NumPy datatype:
You can use the function below to determine the type of any variable: