In this tutorial, we will look at how to check if a number in a Numpy array is a real number or not with the help of some examples. Real numbers in mathematics are numbers that do not have an imaginary part, for example – `21`

, `4.31`

, `2 + 0j`

, etc.

## How to test for a real number in a Numpy array?

You can use the `numpy.isreal()`

function to check (element-wise) if values in a Numpy array are real or not. The following is the syntax –

**📚 Discover Online Data Science Courses & Programs (Enroll for Free)**

**Introductory ⭐**

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# test for real numbers - pass scaler value or numpy array np.isreal(a)

The `numpy.isreal()`

function essentially tests (element-wise) whether the imaginary part of the number is zero or not. It returns a boolean array if you pass an array and if you pass a scaler value, it returns a boolean value.

## Examples

Let’s now look at some examples of using the above function to test for real numbers.

### Example 1 – Check if a number is real or not using `numpy.isreal()`

First, let’s pass scaler values to the `numpy.isreal()`

function.

**Upskill your career right now →**

Let’s apply the `numpy.isreal()`

function on three scaler values – a real number, a complex number with a non-zero imaginary part, and a complex number with the imaginary part as 0.

import numpy as np # check if real print(np.isreal(14)) print(np.isreal(2 + 4j)) print(np.isreal(2 + 0j))

Output:

True False True

We get `True`

as the output for the real number 14 and the complex number having 0 as its imaginary part. And we get `False`

as the output for the complex number with a non-zero imaginary part.

Note that here, even though the value `2 + 0j`

is of `complex`

type, the `numpy.isreal()`

function returns it as a real number (since its imaginary part is 0 which essentially makes it a real number).

If, on the other hand, you want to check whether the value is not of `complex`

type, use the `numpy.isrealobj()`

function.

# check if not complex type object print(np.isrealobj(14)) print(np.isrealobj(2 + 4j)) print(np.isrealobj(2 + 0j))

Output:

True False False

We get `True`

as the output for the real number 14 and `False`

as the output for both the complex type values (irrespective of whether the imaginary part is 0 or not).

**Upskill your career right now →**

### Example 2 – Element-wise check for real number in a Numpy array using `numpy.isreal()`

If you apply the `numpy.isreal()`

function on an array, it will return a boolean array containing `True`

for the values that are real numbers and `False`

for the other values.

Let’s create a 1-D array and apply the `numpy.isreal()`

function to it.

# create a numpy array ar = np.array([1, 2+3j, 2+0j, 4, 5.7, np.nan, np.inf, 1j, 0+0j]) # element-wise check for real number in ar np.isreal(ar)

Output:

array([ True, False, True, True, True, True, True, False, True])

We get a boolean array as an output. You can see that in the boolean array we get `True`

for only the values that test as a real number in the original array.

## Summary

In this tutorial, we looked at how we can use the `numpy.isreal()`

function to check if a number is a real number or not in a Numpy array. Keep in mind that this function checks whether the imaginary part of the number is zero or not. If you want to check whether the number is not of `complex`

type, use the `numpy.isrealobj()`

function instead.

You might also be interested in –

- Practical Guide to Working with Complex Numbers in Python
- Python – Add Two Complex Numbers
- Python – Subtract Two Complex Numbers
- Python – Multiply Two Complex Numbers
- Python – Divide Two Complex Numbers
- Python – Generate a Random Complex Number
- Python – Convert Complex Number to Polar Form
- Python – Get the Phase Angle of a Complex Number
- Python – Get the Absolute Value of a Complex Number
- Numpy – Get the Complex Conjugate
- Numpy – Get the Real Part of a Complex Number
- Numpy – Get the Imaginary Part of a Complex Number
- Numpy – Check If a Number is Complex

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**