w3resource

NumPy: Find the real and imaginary parts of an array of complex numbers

NumPy: Array Object Exercise-15 with Solution

Write a NumPy program to find the real and imaginary parts of an array of complex numbers

Sample Solution:

Python Code:

import numpy as np
x = np.sqrt([1+0j])
y = np.sqrt([0+1j])
print("Original array:x ",x)
print("Original array:y ",y)
print("Real part of the array:")
print(x.real)
print(y.real)
print("Imaginary part of the array:")
print(x.imag)
print(y.imag)

Sample Output:

Original array:x  [ 1.+0.j]
Original array:y  [ 0.70710678+0.70710678j]
Real part of the array:
[ 1.]
[ 0.70710678]
Imaginary part of the array:
[ 0.]
[ 0.70710678]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to convert the values of Centigrade degrees into Fahrenheit degrees. Centigrade values are stored into a NumPy array.
Next: Write a NumPy program to find the number of elements of an array, length of one array element in bytes and total bytes consumed by the elements

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}