Numpy real – Python NumPy real() Function

NumPy real() Function:

Numpy real: The real() function of the NumPy module returns the element-wise real part of the complex argument.

Syntax:

numpy.real(a)

Parameters

a: This is required. It is an array given as input.

Return Value: 

The element-by-element real part of the complex argument is returned by the real() function of the NumPy module

The type of “a” is utilized for the output if “a” is real. The returning type is float if “a” contains complex items.

NumPy real() Function in Python

Example1: (For 1-Dimensional Array)

Approach:

  • Import numpy module using the import keyword
  • Pass some random complex numbers list(1-Dimensional) as an argument to the
    array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the above-given array as an argument to the real() function of the numpy module to get the real part of all the elements of the given array.
  • Store it in another variable.
  • Print the real part of all the elements of the given array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random complex numbers list(1-Dimensional) as an argument to the 
# array() function to create an array. 
# Store it in a variable.
gvn_arry = np.array([1+3j, 4+3j, 2+2j,-1+3j])                     
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
print()
# Pass the above given array as an argument to the real() function of the 
# numpy module to get the real part of all the elements of
# the given array
# Store it in another variable.
rslt = np.real(gvn_arry)
# Print the real part of all the elements of the given array 
print("The real part of all the elements of the given array:")
print(rslt)

Output:

The above given array is:
[ 1.+3.j 4.+3.j 2.+2.j -1.+3.j]

The real part of all the elements of the given array:
[ 1. 4. 2. -1.]

Example2: (For n-Dimensional Array, where n is an integer)

Approach:

  • Import numpy module using the import keyword
  • Pass some random complex numbers list(n-Dimensional) as an argument to the
    array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the above-given array as an argument to the real() function of the numpy module to get the real part of all the elements of the given array.
  • Store it in another variable.
  • Print the real part of all the elements of the given array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random complex numbers list(n-Dimensional) as an argument to the 
# array() function to create an array. 
# Store it in a variable.
gvn_arry = np.array([[1+3j, 4+3j],
                     [2+4j, 5+6j]])           
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
print()
# Pass the above given array as an argument to the real() function of the 
# numpy module to get the real part of all the elements of
# the given array
# Store it in another variable.
rslt = np.real(gvn_arry)
# Print the real part of all the elements of the given array 
print("The real part of all the elements of the given array:")
print(rslt)

Output:

The above given array is:
[[1.+3.j 4.+3.j]
[2.+4.j 5.+6.j]]

The real part of all the elements of the given array:
[[1. 4.]
[2. 5.]]