
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Complex Numbers in Python
Complex numbers is the combination of both real and imaginary components. Sometimes they are written as a + bi, where -
a is the real part or number,
b is the imaginary part,
i is the imaginary unit which is basically a square root of -1.
The imaginary part is written as the letter i. It is basically a square root of -1. Python already has support for complex numbers so it is easy to use them. So in this article, we will explain the basic ideas of complex numbers in Python, like how to create them, change them, and use them.
Creating Complex Numbers
Python generates complex numbers with the real_part + imaginary_partj syntax. Keep in mind that you should use j instead of i for the imaginary unit.
# Creating complex numbers z1 = 3 + 4j z2 = 1 - 2j print("Complex Number z1:", z1) print("Complex Number z2:", z2)
When you run the above code, it will produce the following output -
Complex Number z1: (3+4j) Complex Number z2: (1-2j)
Accessing Real and Imaginary Parts
You can get the real and imaginary parts of a complex integer using the .real and .imag properties. Here is the example for this -
# Accessing real and imaginary parts z1 = 3 + 4j z2 = 1 - 2j print("Real part of z1:", z1.real) print("Imaginary part of z1:", z1.imag) print("Real part of z2:", z2.real) print("Imaginary part of z2:", z2.imag)
After running the above code, you will get -
Real part of z1: 3.0 Imaginary part of z1: 4.0 Real part of z2: 1.0 Imaginary part of z2: -2.0
Performing Operations on Complex Numbers
Among other operations, complex numbers can be added, subtracted, multiplied, and divided. Here is an example of how to perform these operations -
# Define complex numbers z1 = 3 + 4j z2 = 1 - 2j # Adding complex numbers addition = z1 + z2 # Subtracting complex numbers subtraction = z1 - z2 # Multiplying complex numbers multiplication = z1 * z2 # Dividing complex numbers division = z1 / z2 print("Addition:", addition) print("Subtraction:", subtraction) print("Multiplication:", multiplication) print("Division:", division)
When you run this code, it will generate the following output -
Addition: (4+2j) Subtraction: (2+6j) Multiplication: (11-2j) Division: (-1+2j)
Convert to Polar and Rectangular Forms
Polar form is represented as (r, ?) where r is the magnitude and ? is the angle. It is another way to express complex numbers. The cmath package helps you to convert between polar and rectangular (Cartesian) coordinates.
Here is an example -
# Import cmath module import cmath z1 = 3 + 4j # Get the magnitude and angle magnitude, angle = cmath.polar(z1) print("Magnitude of z1:", magnitude) print("Angle of z1 (in radians):", angle)
Output
When you run the following code, you will get a result like this -
Magnitude of z1: 5.0 Angle of z1 (in radians): 0.9272952180016122