Python Program to Add Two Complex Numbers

A complex number is represented by a + bi or a + bj. Python uses a+bj notation for representing complex number.

Python programming language converts the real numbers a and b into complex using the function complex(a,b).

In python, we can set complex number using a = 3 + 5j. After setting like this we can access real part of a can be obtained using a.real and imaginary part of a can be obtained using b.imag.

For Example

>>> a=3+4j
>>> a.real
3.0
>>> a.imag
4.0
>>> complex(1,2)
(1+2j)

Python handles simple operation like addition, subtraction, multiplication and division directly. For more advanced operation on complex numbers you can use cmath library.

This python program adds two complex number given by user and displays the output.

Addition of Two Complex Numbers in Python


# Python program to add two complex number

# Enter first complex number i.e. 3+4j not 3+4i
first = complex(input('Enter first complex number: '))
second = complex(input('Enter first complex number: '))

# Addition of complex number
addition = first + second

# Displaying Sum
print('SUM = ', addition)

Output of Above Program

Enter first complex number: 2+3j
Enter first complex number: 4+6j
SUM =  (6+9j)