/    /  NumPy – Data Types

NumPy – Data Types:

Nympy provides the below dataypes more than what exactly python holds.
Data typeDescription
bool_Boolean (True or False) stored as a byte
int_Default integer type (same as C long; normally either int64 or int32)
intcIdentical to C int (normally int32 or int64)
intpInteger used for indexing (same as C ssize_t; normally either int32 or int64)
int8Byte (-128 to 127)
int16Integer (-32768 to 32767)
int32Integer (-2147483648 to 2147483647)
int64Integer (-9223372036854775808 to 9223372036854775807)
uint8Unsigned integer (0 to 255)
uint16Unsigned integer (0 to 65535)
uint32Unsigned integer (0 to 4294967295)
uint64Unsigned integer (0 to 18446744073709551615)
float_Shorthand for float64.
float16Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
float32Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
float64Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
complex_Shorthand for complex128.
complex64Complex number, represented by two 32-bit floats (real and imaginary components)
complex128Complex number, represented by two 64-bit floats (real and imaginary components)
Let us see some example below:
How identify the datatype ?
Below is the command. we will use the “dtype” method to identify the datatype
>>> import numpy as np

>>> x=np.array([1,2,3,4,5])

>>> x
array([1, 2, 3, 4, 5])

>>> x.dtype
dtype('int32')
What if you want to assign the integers as float datatype ?
Below is the command. Please observe that we have given the dtype as floating datatype.
>>> x=np.array([1,2,3,4,5], dtype=float)

>>> x.dtype
dtype('float64')

>>> x
array([ 1.,  2.,  3.,  4.,  5.])
 Below example shows the floating dataype values.
>>> y=np.array([.1,.2,.3,.4,.5])

>>> y
array([ 0.1,  0.2,  0.3,  0.4,  0.5])

>>> y.dtype
dtype('float64')
Now let us see some more data types too.
This example will show the Boolean datatype.
>>> eq=np.array([True, False])

>>> eq
array([ True, False], dtype=bool)

>>> eq.dtype
dtype('bool')
This example will show the String datatype.
>>> str=np.array(["This", "is", "NumPy"])

>>> str
array(['This', 'is', 'NumPy'],
dtype='<U5')

>>> str.dtype
dtype('<U5')
This example will show the Complex datatype.
>>> j=2

>>> solve=np.array([2+3j, 5+j, 9+2*3j])

>>> solve
array([ 2.+3.j,  7.+0.j,  9.+6.j])

>>> solve.dtype
dtype('complex128')

>>> solve.real
array([ 2.,  7.,  9.])