Hey everyone, welcome back to another exciting tutorial on NumPy.

You all must be familiar with complex numbers, right? If not, let me give you a quick recap. So, complex numbers are special numbers comprised of two parts, the real and the imaginary part. A number **z** of the form **z = x+yi, where x and y are real numbers then the number z,** is said to be a complex number.

That short intro was required for this tutorial as it is the basis of the further understanding of the two functions. Now, can we extract the real part and imaginary part of a complex number just by using simple NumPy functions? Yes, we can do that using the two functions of the NumPy Library i.e. ** numpy.real()** and

**.**

`numpy.imag()`

This is what we will be discussing in the article.

## Understanding Numpy Real

Among a huge variety of functions under the NumPy library, the **numpy real** is a function that **extracts the real part** of a Complex argument.

Let’s take a look at the syntax of the function.

### Syntax of NumPy real

```
numpy.real(val)
```

The input ** val** can be a single complex number as well as a NumPy array of complex numbers.

We will talk about its return type in the code-snippet section as it will be easier for you to understand it along with observing the outputs.

### Working with NumPy real

Let’s write some code to make our understanding of the function clear.

#### 1. NumPy real of a single Complex number

```
import numpy as np
# Passing Complex Numbers as argument to the fucntion
print("The real part of 1+3j is:",np.real(1+3j))
print("The real part of 3j is:",np.real(3j))
print("\n")
# Passing real numbers as argument to the function
print("The real part of 1 is:",np.real(1))
print("The real part of -1.1 is:",np.real(-1.1))
```

#### Output

```
The real part of 1+3j is: 1.0
The real part of 3j is: 0.0
The real part of 1 is: 1
The real part of -1.1 is: -1.1
```

**Note:** Every real number can be expressed as complex numbers. For example, 1 can be expressed as 1+0i, where the imaginary part becomes 0.

In the above examples, we have passed complex numbers as well as real numbers as input to the ** np.real()** function. It is really interesting to note the difference in the outputs in the two situations.

When a complex number is passed as an argument to the function the output is of type **float**. Whereas, when a real number is passed as an argument to the function then the **type** of the output is the **same as** **the input number**. This is only the difference between the return types of the function.

#### 2. NumPy real of a NumPy array of Complex numbers

```
import numpy as np
a = np.array((1+3j , 3j , 1 , 0.5))
b = np.real(a)
print("The input array:\n",a)
print("The real part of the numbers:\n",b)
```

#### Output

```
The input array:
[1. +3.j 0. +3.j 1. +0.j 0.5+0.j]
The real part of the numbers:
[1. 0. 1. 0.5]
```

Let’s understand the above examples. Here, we have created a variable ** a** that stores the NumPy array of four elements out of which two are complex numbers and the other two are real numbers.

In the next line, we used the ** np.real()** function to extract the real part of the elements of the input array. The output of the function is a NumPy array which is stored in the variable

**.**

`b`

In the next two lines, we used two print statements to print the Input array and the output array respectively.

That was all about working with the NumPy real function. Now, we will understand the NumPy imag function.

## About NumPy imag

NumPy imag is also one of the mathematical functions of the NumPy library that **extracts the imaginary part** of the Complex argument.

Its syntax is very similar to the NumPy real function.

### Syntax of NumPy imag

```
numpy.imag(val)
```

The input ** val** can be a single complex number as well as a NumPy array of Complex numbers.

Its return type is **exactly similar** to the return type of the NumPy real function which we discussed in the previous section.

### Working with the NumPy imag

Let us use this function with different types of input values.

#### 1. NumPy imag of a Single Complex number

```
import numpy as np
# Passing Complex Numbers as argument to the fucntion
print("The imaginary part of 1+3j is:",np.imag(1+3j))
print("The imaginary part of 3j is:",np.imag(3j))
print("\n")
# Passing imag numbers as argument to the function
print("The imaginary part of 1 is:",np.imag(1))
print("The imaginary part of -1.1 is:",np.imag(-1.1))
```

#### Output

```
The imaginary part of 1+3j is: 3.0
The imaginary part of 3j is: 3.0
The imaginary part of 1 is: 0
The imaginary part of -1.1 is: 0.0
```

Here, the type of output depends on the type of input given to the function. We can observe that if a complex number is passed as an input then the output of the function is a floating point number whereas if the input is a real number the type of output number depends on the type of the input number.

The function ** np.imag()** extracts the imaginary part of the complex number. The number associated with the term “j” in the complex number is the

**imaginary part**of the complex number.

#### 2. NumPy imag of a NumPy array of Complex numbers

```
import numpy as np
a = np.array((1+3j , 3j , 1 , 0.5))
b = np.imag(a)
print("The input array:\n",a)
print("The imaginary part of the numbers:\n",b)
```

#### Output

```
The input array:
[1. +3.j 0. +3.j 1. +0.j 0.5+0.j]
The imaginary part of the numbers:
[3. 3. 0. 0.]
```

In the above snippet, the first two outputs are clear, but why the other two values in the output array are 0?

Actually, the last two elements of the input array are real numbers that can be written as **1+0j **and **0.5+0j** respectively. So, it is clear that the imaginary part in both numbers is equal to 0. That’s why the last two values in the output array are equal to 0.

So, that was all about using the NumPy real and NumPy imag functions.

## Summary

In this article, we learned about the NumPy real and imag functions along with different types of examples. We also learned about the return types of both functions which was the most interesting part of the article ðŸ™‚

Keep learning and Keep exploring more such articles.