In this Python tutorial, I will explain how to add two complex numbers in Python using many different methods in Python with examples.
In Python, working with complex numbers is not only possible but is also straightforward due to its built-in support for complex number operations.
Python has built-in support for complex numbers, which makes it straightforward to perform operations on them. A complex number in Python can be created using the complex() function or by directly writing it with the j notation for the imaginary part.
For instance:
num1 = 3 + 4j
print('The type of num1 variable is:', type(num1))
num2 = complex(5, -2)
print('The type of num2 variable is:', type(num2))
The output is:
The type of num1 variable is: <class 'complex'>
The type of num2 variable is: <class 'complex'>
This way we can create complex numbers in Python.
To learn more, Read: Complex Numbers in Python
Methods to add two complex numbers in Python
We’ll explore three different methods to add two complex numbers in Python:
- Direct Addition
- Using the complex() function
- Using a Custom Function
Let’s see them one by one using illustrative examples.
Method-1: Addition of two complex numbers in Python using the + operator
This method involves Python’s inherent ability to directly add two complex numbers using the + operator. Just like adding two integers, complex numbers can be added directly without any need for additional functions or libraries in Python.
Scenario: Imagine we’re in New York City and we decide to travel 3 blocks west and then 4 blocks north, which can be represented as the complex number 3 + 4j. Later, we choose to explore a bit more and move 5 blocks west and 2 blocks south, represented by 5 – 2j. Let’s take the sum of these complex values in Python.
nyc_movement1 = 3 + 4j
nyc_movement2 = 5 - 2j
total_movement = nyc_movement1 + nyc_movement2
print('Total movement:', total_movement)
print('type of the sum is:', type(total_movement))
The output is:
Total movement: (8+2j)
type of the sum is: <class 'complex'>
This way we can use the + operator to add two complex numbers in Python.
Method-2: Python add two complex no. using complex() function
The complex() function in Python is used to define a complex number by explicitly providing the real and imaginary components. Once complex numbers are defined using this Python function, they can be added together using the standard arithmetic addition operation in Python.
Scenario: Let’s say we’re at the Grand Canyon. First, we walk 3 meters eastward and then ascend 4 meters to a viewing platform, represented by 3 + 4j. Later, we walk 5 meters east and descend 2 meters to another viewpoint, represented as 5 – 2j. Let’s add these values in Python.
walk1 = complex(3, 4)
walk2 = complex(5, -2)
total_walk = walk1 + walk2
print('Total meter we walked:', total_walk)
print('The type of the sum is:', type(total_walk))
Output:
Total meter we walked: (8+2j)
The type of the sum is: <class 'complex'>
This way we can use the complex() method to add to complex numbers in Python.
Method-3: Function to add two complex numbers in Python
This approach involves creating a user-defined Python function to handle the addition of complex numbers. Typically, the Python function would accept two complex numbers as input arguments and would return their sum. It provides more flexibility and clarity, especially when complex number operations are frequent in a program.
Scenario: At Disneyland in California, we move 3 meters towards the west to reach Space Mountain and then 4 meters north to a souvenir shop, resulting in a movement of 3 + 4j. Later, we head 5 meters west to Adventureland and 2 meters south towards the Pirates of the Caribbean ride, depicted as 5 – 2j. Let’s add these resultant movements(complex numbers) in Python.
def add_movements(move1, move2):
horizontal = move1.real + move2.real
vertical = move1.imag + move2.imag
return complex(horizontal, vertical)
adventure1 = 3 + 4j
adventure2 = 5 - 2j
total_adventure = add_movements(adventure1, adventure2)
print('total adventure is:', total_adventure)
print('Type of the sum is:', type(total_adventure))
Output:
total adventure is: (8+2j)
Type of the sum is: <class 'complex'>
This way we can use the function in Python to add two complex numbers.
Conclusion
This tutorial explains how to add two complex numbers in Python using three different methods. Like using the + operator, complex() function, or function in Python with demonstrative examples. In the process, we have also seen what is a complex number in Python.
You may also like to read:
- How to check if a variable is a number in Python?
- How to reverse a number in Python
- How to calculate square root in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.