Return the imaginary part of the complex argument in Python

PythonNumpyServer Side ProgrammingProgramming

To return the imaginary part of the complex argument, use the numpy.imag() method. The method returns the imaginary component of the complex argument. If val is real, the type of val is used for the output. If val has complex elements, the returned type is float. The 1st parameter, val is the input array

Steps

At first, import the required libraries -

import numpy as np

Create an array using the array() method −

arr = np.array([36.+5.j , 27.+3.j , 68.+2.j , 23.+7.j])

Display the array −

print("Our Array...\n",arr)

Check the Dimensions −

print("\nDimensions of our Array...\n",arr.ndim)

Get the Datatype −

print("\nDatatype of our Array object...\n",arr.dtype)

Get the Shape −

print("\nShape of our Array...\n",arr.shape)

To return the imaginary part of the complex argument, use the numpy.imag() method in Python. The method returns the imaginary component of the complex argument. If val is real, the type of val is used for the output. If val has complex elements, the returned type is float. The 1st parameter, val is the input array −

print("\nResult (Imaginary part)...\n",np.imag(arr))

Example

import numpy as np

# Create an array using the array() method
arr = np.array([36.+5.j , 27.+3.j , 68.+2.j , 23.+7.j])

# Display the array
print("Our Array...\n",arr)

# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)

# Get the Shape
print("\nShape of our Array...\n",arr.shape)

# To return the imaginary part of the complex argument, use the numpy.imag() method in Python
print("\nResult (Imaginary part)...\n",np.imag(arr))

Output

Our Array...
[36.+5.j 27.+3.j 68.+2.j 23.+7.j]

Dimensions of our Array...
1

Datatype of our Array object...
complex128

Shape of our Array...
(4,)

Result (Imaginary part)...
[5. 3. 2. 7.]
raja
Updated on 25-Feb-2022 06:02:43

Advertisements