subtract two complex numbers in Python

Python – Subtract Two Complex Numbers

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

subtract two complex numbers in Python

When you subtract two complex numbers, for example, subtracting c+dj from a+bj, the resulting complex number is (a-c)+(b-d)j.

How to subtract two or more complex numbers in Python?

You can use the subtraction operator - to subtract two complex numbers in Python. The following is the syntax –

# subtract complex number z2 from z1
z1 - z2

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

Examples

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

Example 1 – Subtract two complex numbers

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

# two complex numbers
z1 = 3+5j
z2 = 1+4j
# subtract complex number z2 from z1
print(z1-z2)

Output:

(2+1j)

You can see that in the resulting complex number, the real and the imaginary parts result from subtracting the corresponding real and imaginary parts of z2 from z1.

📚 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.

Note that, unlike the addition operation, the subtraction operation is not commutative. Meaning that the result of the operation z1 - z2 may not necessarily be the same as that of the operation z2 - z1.

# two complex numbers
z1 = 3+5j
z2 = 1+4j
# subtract complex number z1 from z2
print(z2-z1)

Output:

(-2-1j)

You can see that here, the complex number resulting from z2 - z1 is different from what we got in the above example when performing z1 - z2.

Example 2 – Subtract a real number from a complex number

Let’s now subtract a real number (a number without any imaginary component) from a complex number (having non-zero real and imaginary parts).

# a complex number and a real number
z1 = 1+2j
z2 = 5
# subtract z2 from z1
print(z1-z2)

Output:

(-4+2j)

We get the resulting complex number. Note that since, here, z2 didn’t have any imaginary part, its imaginary part is treated as zero.

Example 3 – Subtract an imaginary number from a complex number

Let’s now subtract an imaginary number from a complex number.

# an imaginary number and a complex number
z1 = 1+2j
z2 = 3j
# subtract z2 from z1
print(z1-z2)

Output:

(1-1j)

We get the resulting complex number. Note that since, here, z2 didn’t have any real part, its real part is treated as zero.

Summary

In this tutorial, we looked at how we can use the subtraction operator, - in Python to subtract two complex numbers in Python. The components of the resulting complex number are obtained from the subtraction of the corresponding real and imaginary components. Also, keep in mind that the subtraction operation is not commutative, that is, z1-z2 != z2-z1.

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