Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

NumPy Data Types

Data Types in Python

By default, Python includes the following data types:

  • Strings: Used to represent text data, enclosed in quotation marks (e.g., “ABCD”).
  • Integer: Used to represent whole numbers (e.g., -1, -2, -3).
  • Float: Used to represent real numbers (e.g., 1.2, 42.42).
  • Boolean: Used to represent True or False values.
  • Complex: Used to represent complex numbers (e.g., 1.0 + 2.0j, 1.5 + 2.5j).

Data Types in NumPy

NumPy offers additional data types and uses single-character identifiers to represent them, such as ‘i’ for integers and ‘u’ for unsigned integers.

Here is a list of all data types in NumPy along with their corresponding characters:

  • i – integer
  • b – boolean
  • u – unsigned integer
  • f – float
  • c – complex float
  • m – timedelta
  • M – datetime
  • O – object
  • S – string
  • U – Unicode string
  • V – fixed-size memory block for other types (void)

Checking the Data Type of an Array

The NumPy array object includes a property called dtype that provides the data type of the array.

Example

Retrieve the data type of an array object.

import numpy as np

arr = np.array([1234])

print(arr.dtype)

Example

Obtain the data type of an array that contains strings.

import numpy as np

arr = np.array([‘apple’‘banana’‘cherry’])

print(arr.dtype)