NumPy real()
The numpy.real() function extracts the real part of a complex number or an array of complex numbers. If the input is already real, it remains unchanged.
Syntax
</>
Copy
numpy.real(val)
Parameters
| Parameter | Type | Description |
|---|---|---|
val | array_like | Input array, which can contain real or complex numbers. |
Return Value
Returns an array or scalar containing the real component of the input. If the input is real, it remains unchanged. If the input contains complex numbers, the output will be of type float.
Examples
1. Extracting the Real Part of a Complex Number
Here, we extract the real part from a single complex number.
</>
Copy
import numpy as np
# Define a complex number
complex_num = 3 + 4j
# Extract the real part
real_part = np.real(complex_num)
# Print the result
print("Real part of", complex_num, ":", real_part)
Output:
Real part of (3+4j) : 3.0
