NumPy Datatypes

The NumPy provides a higher range of numeric data types than that provided by the Python. A list of numeric data types is given in the following table.

SNData typeDescription
1bool_It represents the boolean value indicating true or false. It is stored as a byte.
2int_It is the default type of integer. It is identical to long type in C that contains 64 bit or 32-bit integer.
3intcIt is similar to the C integer (c int) as it represents 32 or 64-bit int.
4intpIt represents the integers which are used for indexing.
5int8It is the 8-bit integer identical to a byte. The range of the value is -128 to 127.
6int16It is the 2-byte (16-bit) integer. The range is -32768 to 32767.
7int32It is the 4-byte (32-bit) integer. The range is -2147483648 to 2147483647.
8int64It is the 8-byte (64-bit) integer. The range is -9223372036854775808 to 9223372036854775807.
9uint8It is the 1-byte (8-bit) unsigned integer.
10uint16It is the 2-byte (16-bit) unsigned integer.
11uint32It is the 4-byte (32-bit) unsigned integer.
12uint64It is the 8 bytes (64-bit) unsigned integer.
13float_It is identical to float64.
14float16It is the half-precision float. 5 bits are reserved for the exponent. 10 bits are reserved for mantissa, and 1 bit is reserved for the sign.
15float32It is a single precision float. 8 bits are reserved for the exponent, 23 bits are reserved for mantissa, and 1 bit is reserved for the sign.
16float64It is the double precision float. 11 bits are reserved for the exponent, 52 bits are reserved for mantissa, 1 bit is used for the sign.
17complex_It is identical to complex128.
18complex64It is used to represent the complex number where real and imaginary part shares 32 bits each.
19complex128It is used to represent the complex number where real and imaginary part shares 64 bits each.

NumPy dtype

All the items of a numpy array are data type objects also known as numpy dtypes. A data type object implements the fixed size of memory corresponding to an array.

We can create a dtype object by using the following syntax.

The constructor accepts the following object.

Object: It represents the object which is to be converted to the data type.

Align: It can be set to any boolean value. If true, then it adds extra padding to make it equivalent to a C struct.

Copy: It creates another copy of the dtype object.

Example 1

Output:

int32

Example 2

Output:

int32

Creating a Structured data type

We can create a map-like (dictionary) data type which contains the mapping between the values. For example, it can contain the mapping between employees and salaries or the students and the age, etc.

Consider the following example.

Example 1

Output:

[('salary', '

Example 2

Output:

[(10000.12,) (20000.5 ,)]





Latest Courses