0% found this document useful (0 votes)
2 views3 pages

Python Data Types

NumPy offers a variety of optimized data types (dtypes) for numerical computing, which have fixed sizes for improved memory efficiency and speed. Common data types include various integers, floating-point numbers, complex numbers, booleans, and strings. The advantages of using NumPy data types include faster computations and better performance for large datasets, making them essential in fields like data science and machine learning.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Python Data Types

NumPy offers a variety of optimized data types (dtypes) for numerical computing, which have fixed sizes for improved memory efficiency and speed. Common data types include various integers, floating-point numbers, complex numbers, booleans, and strings. The advantages of using NumPy data types include faster computations and better performance for large datasets, making them essential in fields like data science and machine learning.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NumPy provides its own set of data types ( dtypes ) that are optimized for numerical

computing. Unlike Python's built-in data types, NumPy data types have fixed sizes, making
them more memory-efficient and faster for array operations.

Common NumPy Data Types


Data Type Description Example

int8 8-bit signed integer np.int8(10)

int16 16-bit signed integer np.int16(1000)

int32 32-bit signed integer np.int32(100000)

int64 64-bit signed integer np.int64(1000000000)

uint8 8-bit unsigned integer np.uint8(255)

uint16 16-bit unsigned integer np.uint16(65535)

uint32 32-bit unsigned integer np.uint32(4000000000)

uint64 64-bit unsigned integer np.uint64(10000000000)

float16 Half-precision floating point np.float16(3.14)

float32 Single-precision floating point np.float32(3.14)

float64 Double-precision floating point np.float64(3.14)

complex64 Complex number (32-bit real + 32-bit np.complex64(2+3j)

imaginary)

complex128 Complex number (64-bit real + 64-bit np.complex128(2+3j)

imaginary)

bool_ Boolean np.bool_(True)

str_ Unicode string np.str_("Python")

bytes_ Byte string np.bytes_(b"Hello")

Example Program
Python Run

import numpy as np

a = np.int32(25)
b = np.float64(12.5)
c = np.complex128(3 + 4j)
d = np.bool_(True)
e = np.str_("NumPy")

print(a, type(a))
print(b, type(b))
print(c, type(c))
print(d, type(d))
print(e, type(e))

Output

Python Run

25 <class 'numpy.int32'>
12.5 <class 'numpy.float64'>
(3+4j) <class 'numpy.complex128'>
True <class '[Link]'>
NumPy <class 'numpy.str_'>

Checking the Data Type of a NumPy Array

Python Run

import numpy as np

arr = [Link]([1, 2, 3], dtype=np.int64)

print(arr)
print([Link])

Output

Python Run

[1 2 3]
int64

Converting a NumPy Array to Another Data Type

Python Run
import numpy as np

arr = [Link]([1, 2, 3])

arr_float = [Link](np.float32)

print(arr_float)
print(arr_float.dtype)

Output

Python Run

[1. 2. 3.]
float32

Categories of NumPy Data Types


1. Integer Types: int8 , int16 , int32 , int64
2. Unsigned Integer Types: uint8 , uint16 , uint32 , uint64
3. Floating-Point Types: float16 , float32 , float64
4. Complex Number Types: complex64 , complex128
5. Boolean Type: bool_
6. String Types: str_ , bytes_

Key Advantages of NumPy Data Types


Fixed memory size for efficient storage.
Faster numerical computations than Python's built-in types.
Better performance for large arrays and matrices.
Widely used in data science, machine learning, scientific computing, and deep
learning.

You might also like