A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/numpy-isreal-python/ below:

numpy.isreal() in Python - GeeksforGeeks

numpy.isreal() in Python

Last Updated : 21 Jun, 2025

numpy.isreal() tests element-wise whether each value in the input array is a real number (i.e., not complex). It returns a Boolean result as a boolean array. Example:

Python
import numpy as np
a = np.array([1+0j, 2+3j, 5, 4.5, 7j])
res = np.isreal(a)
print(res)

Output
[ True False  True  True False]

Explanation: np.isreal() returns True for real numbers (even if written with +0j) and False for complex numbers with a non-zero imaginary part like 2+3j or 7j.

Syntax

numpy.isreal(x)]

Parameters: x is the input array or scalar (can be complex).

Returns: A Boolean array of the same shape as x.

Examples

Example 1: In this example, we use np.isreal() with a purely real-valued array.

Python
import numpy as np
a = np.array([1, 2.5, 0, -3])
res = np.isreal(a)
print(res)

Output
[ True  True  True  True]

Explanation: All elements in the array are real numbers, so np.isreal() returns True for each one.

Example 2: In this example, we use a 2D array with both real and complex values to check element-wise real status.

Python
import numpy as np
a = np.array([[1+0j, 3+4j], [5, 0-2j]])
res = np.isreal(a)
print(res)

Output
[[ True False]
 [ True False]]

Explanation:

Example 3: In this example, we filter out only real values from an array that includes complex numbers.

Python
import numpy as np
a = np.array([2+0j, 3+1j, 4, 5-2j])
res = a[np.isreal(a)]
print(res)

Explanation: We use the Boolean mask returned by np.isreal() to extract only the real values from the array. Complex numbers are excluded.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4