# Numpy – Get the Real Part of a Complex Number

In this tutorial, we will look at how to get the real part of a complex number in a Numpy array. For example for the complex number `a + bj`, `a` is the real part and `b` is the imaginary part.

## How to get the real part of complex values in a Numpy array?

You can use the `numpy.real()` function to get (element-wise) the real part of complex numbers in a Numpy array. The following is the syntax –

```# get the real part - pass scaler value or numpy array
np.real(a)```

The `numpy.real()` function essentially returns (element-wise) the real component of the complex argument.

If you pass a scalar value, it returns the real part of that value. And, if you pass a Numpy array, it returns the array of real parts of the array elements.

## Examples

Let’s now look at some examples of using the above function to extract the real part of complex numbers.

Highlighted programs for you

Flatiron School

Flatiron School

Data Science Bootcamp
Product Design UX/UI Bootcamp

University of Maryland Global Campus

University of Maryland Global Campus

Cloud Computing Systems Master's
Digital Forensics & Cyber Investigation Master's

Creighton University

Creighton University

Health Informatics Master's

### Example 1 – Extract the real part for a scalar value using `numpy.real()`

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

Let’s apply the `numpy.real()` function on three scalar values – a real number, a complex number with a non-zero real part, and a complex number with the real part as 0.

```import numpy as np

# get the real part
print(np.real(14))
print(np.real(2 + 4j))
print(np.real(3j))```

Output:

```14
2.0
0.0```

We get the real component for all the scalar values irrespective of whether we passed a real or a complex number as an argument.

### Example 2 – Element-wise extract the real part in a Numpy array using `numpy.real()`

If you apply the `numpy.real()` function on an array, it will return a Numpy array of the extracted real components for the elements inside the array.

Let’s create a 1-D array and apply the `numpy.real()` 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 extract the real part in ar
np.real(ar)```

Output:

`array([1. , 2. , 2. , 4. , 5.7, nan, inf, 0. , 0. ])`

We get a Numpy array containing the real component of each value in the array `ar`.

## Summary – Real part of complex numbers in Numpy array

In this tutorial, we looked at how we can use the `numpy.real()` function to extract the real component of a complex number in a Numpy array. Keep in mind that if you apply this function on a scalar value, it returns the real component whereas if you apply it on a Numpy array, it returns the array of real components.

You might also be interested in –

• 