
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Change the imaginary part of the complex argument in Python
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. We will also update the imaginary part of the complex argument using array.img.
Steps
At first, import the required libraries −
import numpy as np
Create an array using the array() method −
arr = np.array([36.+1.j , 27.+2.j , 68.+3.j , 23.+2.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("\nImaginary part...\n",np.imag(arr))
Change the imaginary part −
arr.imag = 5 print("\nUpdated result...\n",arr)
Example
import numpy as np # Create an array using the array() method arr = np.array([36.+1.j , 27.+2.j , 68.+3.j , 23.+2.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("\nImaginary part...\n",np.imag(arr)) # Change the imaginary part arr.imag = 5 print("\nUpdated result...\n",arr)
Output
Our Array... [36.+1.j 27.+2.j 68.+3.j 23.+2.j] Dimensions of our Array... 1 Datatype of our Array object... complex128 Shape of our Array... (4,) Imaginary part... [1. 2. 3. 2.] Updated result... [36.+5.j 27.+5.j 68.+5.j 23.+5.j]
- Related Articles
- Return the imaginary part of the complex argument in Python
- Change the real part of the complex argument in Python
- Return the real part of the complex argument in Python
- Swift Program to get the imaginary part of the given complex number
- C++ Program to get the imaginary part of the given complex number
- Return the angle of the complex argument in Python
- Return the angle of the complex argument in degrees in Python
- Return the angle of the complex argument in radians in Python
- Get the Imaginary part from the masked array in Numpy
- Return real parts if input is complex with all imaginary parts close to zero in Python
- How to construct a complex tensor with the given real and imaginary parts in PyTorch?
- Haskell Program to create Complex numbers from given imaginary parts
- Golang Program to create Complex numbers from given imaginary parts using In-build Library Function
- Return the square of the complex-value input in Python
- Argument of an Exception in Python
