add two complex numbers in python

Python – Add Two Complex Numbers

In this tutorial, we will look at how to perform addition on complex numbers in Python with the help of some examples.

add two complex numbers in python

When you add two complex numbers, for example a+bj and c+dj, the resulting complex number is (a+c)+(b+d)j.

How to add two or more complex numbers in Python?

You can use the addition operator + to add two or more complex numbers in Python. The following is the syntax –

# add complex numbers z1 and z2
z1+z2

It gives you the complex number resulting from the addition of the corresponding real and imaginary coefficients together.

Examples

Let’s now look at some examples of using the above syntax to add some numbers together.

Example 1 – Add two complex numbers

Let’s add two complex numbers (both having non-zero real and imaginary parts) together using the + operator.

# two complex numbers
z1 = 2+3j
z2 = 3+4j
# add complex numbers
print(z1+z2)

Output:

(5+7j)

You can see that in the resulting complex number, the real and the imaginary parts are the sums of the corresponding components from the individual complex numbers in the addition operation.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Example 2 – Add a real number with a complex number

Let’s now add a real number (a number without any imaginary component) and a complex number together.

# a real number and a complex number
z1 = -3
z2 = 3+4j
# add complex numbers
print(z1+z2)

Output:

4j

We get the resulting complex number.

You can see that in this specific example, the resulting complex number is imaginary. This is because the real components cancel each other out during addition as they are of the same magnitude and the opposite sign.

Note that adding a real number with a complex number may or may not result in a completely imaginary number, it all depends on the real and the imaginary components of the complex numbers being added.

Example 3 – Add an imaginary number with a complex number

Let’s now add an imaginary number with a complex number.

# an imaginary number and a complex number
z1 = -3j
z2 = 3+4j
# add complex numbers
print(z1+z2)

Output:

(3+1j)

We get the resulting complex number.

Summary

In this tutorial, we looked at how we can use the addition operator, + in Python to add two or more complex numbers in Python. The components of the resulting complex number are obtained from the addition of the corresponding real and imaginary components.

You may also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top