In this tutorial, we will look at how to subtract two complex numbers in Python with the help of some examples.
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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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 –
- Practical Guide to Working with Complex Numbers in Python
- Python – Add Two Complex Numbers
- Python – Multiply Two Complex Numbers
- Python – Divide Two Complex Numbers
- Python – Generate a Random Complex Number
- Python – Convert Complex Number to Polar Form
- Python – Get the Phase Angle of a Complex Number
- Python – Get the Absolute Value of a Complex Number
- Numpy – Get the Complex Conjugate
- Numpy – Get the Real Part of a Complex Number
- Numpy – Get the Imaginary Part of a Complex Number
- Numpy – Check If a Number is Real
- Numpy – Check If a Number is Complex
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.