What is the numpy.real() function in NumPy?

Overview

The real() function in NumPy is used to return the real part of the complex argument that is passed to it.

Syntax

numpy.real(val)
Syntax for the "real()" function in NumPy

Parameter value

The real() function takes a single parameter value, val, which represents an input array that has complex values.

Return value

The real() function returns the real component of the val parameter value that is passed to it.

Example

import numpy as np
# creating an input array of complex values
x = np.array([1+2j, 2+3j, 1+5j])
# implementing the real) function
myarray = np.real(x)
print(x)
print(myarray)

Explanation

  • Line 1: We import the numpy module.
  • Line 4: We create an array x that has complex values, using the array() function.
  • Line 7: We implement the real() function on the input array. We assign the result to a variable called myarray.
  • Line 9: We print the variable x.
  • Line 10: We print the variable myarray.

Free Resources