divide two complex numbers in python

Python – Divide Two Complex Numbers

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

divide two complex numbers in python

The above image shows how we can simplify the computation of the division of complex numbers. The idea is to multiply both the numerator and the denominator with the complex conjugate of the denominator. This way the denominator becomes real and grouping the real and imaginary components becomes easier.

How to divide complex numbers in Python?

Performing the complex number division is very simple and direct in Python. You can use the division operator / to divide two complex numbers together. The following is the syntax –

# divide complex numbers z1 and z2
z1/z2

It gives you the complex number resulting from dividing the complex number z1 with the complex number z2.

Examples

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

Example 1 – Divide two complex numbers

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

# two complex numbers
z1 = 2+3j
z2 = 3+4j
# divide z1 by z2
print(z1/z2)

Output:

(0.72+0.04j)

We get the resulting complex number.

📚 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 the division operation in mathematics is not commutative, that is,
z1/z2 != z2/z1

# divide z2 by z1
print(z2/z1)

Output:

(1.3846153846153848-0.07692307692307697j)

We get a different result from what we got above.

Example 2 – Divide a complex number by a real number

Let’s now divide a complex number by a real number (a number without any imaginary component).

# a complex number and a real number
z1 = 2+8j
z2 = 2
# divide z1 by z2
print(z1/z2)

Output:

(1+4j)

Dividing a complex number by a real number is straightforward to understand. The real number divides both the real and the imaginary components of the complex number to get our resulting complex number.

Example 3 – Divide a complex number by an imaginary number

Let’s now divide a complex number by an imaginary number.

# a complex number and an imaginary number
z1 = 2+8j
z2 = 4j
# divide z1 by z2
print(z1/z2)

Output:

(2-0.5j)

We get the resulting complex number.

Summary

In this tutorial, we looked at how we can use the division operator, / in Python to divide two complex numbers in Python. Keep in mind that the division operation is not commutative, meaning 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