Complex Numbers in Python | Set 1 (Introduction)
Last Updated :
21 Aug, 2024
Not only real numbers, Python can also handle complex numbers and its associated functions using the file "cmath". Complex numbers have their uses in many applications related to mathematics and python provides useful tools to handle and manipulate them. Converting real numbers to complex number An complex number is represented by " x + yi ". Python converts the real numbers x and y into complex using the function complex(x,y). The real part can be accessed using the function real() and imaginary part can be represented by imag().
Python
# Python code to demonstrate the working of
# complex(), real() and imag()
# importing "cmath" for complex number operations
import cmath
# Initializing real numbers
x = 5
y = 3
# converting x and y into complex number
z = complex(x, y)
# printing real and imaginary part of complex number
print("The real part of complex number is:", z.real)
print("The imaginary part of complex number is:", z.imag)
OutputThe real part of complex number is: 5.0
The imaginary part of complex number is: 3.0
An alternative way to initialize a complex number
Below is the implementation of how can we make complex no. without using complex() function.
Python
# An alternative way to initialize complex numbers"
# importing "cmath" for complex number operations
import cmath
# Initializing complex number
z = 5+3j
# Print the parts of Complex No.
print("The real part of complex number is : ", end="")
print(z.real)
print("The imaginary part of complex number is : ", end="")
print(z.imag)
OutputThe real part of complex number is : 5.0
The imaginary part of complex number is : 3.0
Explanation: Phase of complex number Geometrically, the phase of a complex number is the angle between the positive real axis and the vector representing a complex number. This is also known as the argument of a complex number. Phase is returned using phase(), which takes a complex number as an argument. The range of phase lies from -pi to +pi. i.e from -3.14 to +3.14.
Python
# importing "cmath" for complex number operations
import cmath
# Initializing real numbers
x = -1.0
y = 0.0
# converting x and y into complex number
z = complex(x, y)
# printing phase of a complex number using phase()
print("The phase of complex number is:", cmath.phase(z))
OutputThe phase of complex number is: 3.141592653589793
Converting from polar to rectangular form and vice versa Conversion to polar is done using polar(), which returns a pair(r,ph) denoting the modulus r and phase angle ph. modulus can be displayed using abs() and phase using phase(). A complex number converts into rectangular coordinates by using rect(r, ph), where r is modulus and ph is phase angle. It returns a value numerically equal to r * (math.cos(ph) + math.sin(ph)*1j)
Python
# importing "cmath" for complex number operations
import cmath
import math
# Initializing real numbers
x = 1.0
y = 1.0
# converting x and y into complex number
z = complex(x, y)
# converting complex number into polar using polar()
w = cmath.polar(z)
# printing modulus and argument of polar complex number
print("The modulus and argument of polar complex number is:", w)
# converting complex number into rectangular using rect()
w = cmath.rect(1.4142135623730951, 0.7853981633974483)
# printing rectangular form of complex number
print("The rectangular form of complex number is:", w)
OutputThe modulus and argument of polar complex number is: (1.4142135623730951, 0.7853981633974483)
The rectangular form of complex number is: (1.0000000000000002+1j)
Complex Numbers in Python | Set 2 (Important Functions and Constants)
Similar Reads
Complex Numbers in Python | Set 2 (Important Functions and Constants) Introduction to python complex numbers: Complex Numbers in Python | Set 1 (Introduction) Some more important functions and constants are discussed in this article. Operations on complex numbers : 1. exp() :- This function returns the exponent of the complex number mentioned in its argument. 2. log(x
4 min read
Array in Python | Set 1 (Introduction and Functions) Other than some generic containers like lists, Python in its definition can also handle containers with specified data types. The array can be handled in Python by a module named "array". They can be useful when we have to manipulate only specific data type values. Properties of ArraysEach array ele
7 min read
How to plot a complex number in Python using Matplotlib ? In this article we will learn how to plot complex number in Python using Matplotlib. Let's discuss some concepts : Matplotlib : Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designe
3 min read
set() Constructor in Python In Python, the set() constructor is used to create a set object. A set is a built-in data type that stores an unordered collection of unique elements. The set() constructor can be used to create an empty set or convert other data types (like lists, tuples, or strings) into a set.Example:Python# Exam
2 min read
Complexity Cheat Sheet for Python Operations Python built-in data structures like lists, sets, and dictionaries provide a large number of operations making it easier to write concise code However, not understanding the complexity of these operations can sometimes cause your programs to run slower than expected. This cheat sheet is designed to
2 min read
Python complex() Function Python provides a built-in function called complex() to handle complex numbers. A complex number consists of a real part and an imaginary part, represented in the form: a + bjWhere,a is the real partb is the imaginary partj is the imaginary unit, which is equal to the square root of -1python uses 'j
2 min read