Return the imaginary part of the complex argument in Python

To return the imaginary part of a complex number or array, use numpy.imag(). This method extracts the imaginary component from complex numbers. If the input is real, it returns the same type; if complex, it returns float values representing the imaginary parts.

Syntax

numpy.imag(val)

Parameters:

  • val − Input array or scalar with complex numbers

Returns: Array of imaginary parts as float values

Basic Example with Single Complex Number

import numpy as np

# Single complex number
z = 5 + 3j
print("Complex number:", z)
print("Imaginary part:", np.imag(z))
Complex number: (5+3j)
Imaginary part: 3.0

Working with Complex Arrays

import numpy as np

# Create an array of complex numbers
arr = np.array([36.+5.j, 27.+3.j, 68.+2.j, 23.+7.j])

print("Original array:")
print(arr)
print("\nDatatype:", arr.dtype)
print("Shape:", arr.shape)

# Extract imaginary parts
imaginary_parts = np.imag(arr)
print("\nImaginary parts:")
print(imaginary_parts)
print("Type of result:", type(imaginary_parts[0]))
Original array:
[36.+5.j 27.+3.j 68.+2.j 23.+7.j]

Datatype: complex128
Shape: (4,)

Imaginary parts:
[5. 3. 2. 7.]
Type of result: <class 'numpy.float64'>

Real Numbers vs Complex Numbers

import numpy as np

# Real numbers
real_arr = np.array([1, 2, 3, 4])
print("Real array:", real_arr)
print("Imaginary part of real numbers:", np.imag(real_arr))

# Mixed array (real and complex)
mixed_arr = np.array([1+0j, 2+3j, 4, 5+1j])
print("\nMixed array:", mixed_arr)
print("Imaginary parts:", np.imag(mixed_arr))
Real array: [1 2 3 4]
Imaginary part of real numbers: [0. 0. 0. 0.]

Mixed array: [1.+0.j 2.+3.j 4.+0.j 5.+1.j]
Imaginary parts: [0. 3. 0. 1.]

Common Use Cases

Input Type Return Type Example
Real scalar Same as input np.imag(5) ? 0.0
Complex scalar float np.imag(3+4j) ? 4.0
Complex array float array np.imag([1+2j, 3+4j]) ? [2., 4.]

Conclusion

Use numpy.imag() to extract imaginary parts from complex numbers or arrays. The function returns float values representing the imaginary components, making it essential for complex number operations in NumPy.

Updated on: 2026-03-26T19:20:11+05:30

281 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements