
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Determine common type following standard coercion rules in Python
To determine common type following standard coercion rules, use the numpy.find_common_type() method in Python numpy. The 1st argument is a list of dtypes or dtype convertible objects representing arrays. The 2nd argument is A list of dtypes or dtype convertible objects representing scalars.
The find_common_type() method returns the common data type, which is the maximum of array_types ignoring scalar_types, unless the maximum of scalar_types is of a different kind (dtype.kind). If the kind is not understood, then None is returned.
Steps
At first, import the required library −
import numpy as np
Using the find_common_type() method in Numpy. Determine common type following standard coercion rules −
print("Result...",np.find_common_type([np.float32], [np.int64, np.float64])) print("Result...",np.find_common_type([], [np.int64, np.float32, complex])) print("Result...",np.find_common_type([np.float32], [np.int64, np.float64])) print("Result...",np.find_common_type([np.float32], [complex])) print("Result...",np.find_common_type([np.float64], [complex])) print("Result...",np.find_common_type(['f4', 'i4'], ['c8'])) print("Result...",np.find_common_type([np.int64], [complex])) print("Result...",np.find_common_type([np.int64], [np.float64]))
Example
import numpy as np # To determine common type following standard coercion rules, use the numpy.find_common_type() method in Python numpy # The 1st argument is a list of dtypes or dtype convertible objects representing arrays. # The 2nd argument is A list of dtypes or dtype convertible objects representing scalars. print("Using the find_common_type() method in Numpy\n") # Determine common type following standard coercion rules print("Result...",np.find_common_type([np.float32], [np.int64, np.float64])) print("Result...",np.find_common_type([], [np.int64, np.float32, complex])) print("Result...",np.find_common_type([np.float32], [np.int64, np.float64])) print("Result...",np.find_common_type([np.float32], [complex])) print("Result...",np.find_common_type([np.float64], [complex])) print("Result...",np.find_common_type(['f4', 'i4'], ['c8'])) print("Result...",np.find_common_type([np.int64], [complex])) print("Result...",np.find_common_type([np.int64], [np.float64]))
Output
Using the find_common_type() method in Numpy Result... float32 Result... complex128 Result... float32 Result... complex128 Result... complex128 Result... complex128 Result... complex128 Result... float64
- Related Articles
- What is JavaScript type coercion?
- Determine the type of an image in Python?
- JavaScript : Why does % operator work on strings? - (Type Coercion)
- Determine type of sound file using Python (sndhdr)
- Return the type that results from applying the NumPy type promotion rules to the arguments in Python
- Determine the type of an image using Python (imghdr)
- How does Implicit coercion differ from Explicit coercion in JavaScript?
- Determine whether the given object represents a scalar data-type in Python
- Argument Coercion in C/C++?
- Standard Data Types in Python
- Determine if the type in the first argument is a subclass of second in Python
- Return a scalar type which is common to the input arrays in Python
- Inplace vs Standard Operators in Python
- Standard errno system symbols in Python
- Program to find nth sequence after following the given string sequence rules in Python

Advertisements