NUMPY Tutorial



NumPy ARRAY DTYPES


πŸ”£ NumPy Array Data Types (dtype)

Every NumPy array has a data type (called dtype) that tells us what kind of elements are stored in the array β€” for example, integers, floats, or strings.

βœ… Common NumPy Data Types

  • int32, int64 – Integer numbers
  • float32, float64 – Floating point numbers
  • bool_ – Boolean values (True or False)
  • complex64, complex128 – Complex numbers
  • str_ or unicode_ – Strings

πŸ“Œ Check the Data Type of an Array

Use array.dtype to check the data type of a NumPy array:

import numpy as np

arr = np.array([10, 20, 30])
print(arr.dtype)

Output:

int64

🎯 Specify a Data Type Using dtype

You can specify the data type when creating an array:

# Create float array
arr = np.array([1, 2, 3], dtype='float32')
print(arr)

Output:

[1. 2. 3.]

πŸ”„ Convert Data Type of Existing Array

Use astype() to convert an array’s data type.

arr = np.array([1.7, 2.8, 3.9])
int_arr = arr.astype('int')
print(int_arr)

Output:

[1 2 3]
⚠️ Note: Type conversion using astype() creates a new array β€” it doesn’t modify the original array.

🧠 Why Is dtype Important?

  • Improves performance by optimizing memory usage
  • Ensures type safety when performing operations
  • Lets you control precision in scientific computing
βœ… Next Up: Array Indexing & Slicing in NumPy ➑️

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review