Most programming languages have no support or very limited support for complex numbers, forcing to learn specialized tools like MATLAB or to find another third-party library.
Python is however a rare exception when it comes to programming languages, as it comes with complex numbers built in.
Despite the name, Python Complex Numbers aren’t complicated, so let’s get started by taking a look at what Complex Numbers are.
NOTE: This tutorial requires that Python is already installed, as well as an IDE (Integrated Development Environment), such as PyCharm. It’s also recommended that you have a basic understanding of How to Use Python if this is your first time learning Python.
With that said, let’s get started with this free Python Complex Numbers tutorial.
What is a Complex Number in Python?
Python Complex numbers are great for working out practical problems.
They are generally used in vector graphics and sound frequency analysis, but can also help in drawing fractals.
Complex Numbers are made up of Real Numbers and Imaginary Numbers.
So, in order to understand Complex Numbers, we first need to understand what Real and Imaginary Numbers are.
What are Real Numbers?
Real Numbers are numbers that include:
- Whole Numbers – Non Decimal, Non Fraction Numbers
- 0
- 1
- 387
- 6584
- 86037
- Ration Numbers – CAN be written as a Ratio such as a simple fraction or Repeating Decimal Value
- 4/5
- 0.333…
- 1.5
- Irrational Numbers – CANNOT be written as Ratio such as a Fraction or Repeating Decimal Value
- π (pi) = 3.1415926535897932384626433832795…
- √2 = 1.41421356237309…
- e (Euler) = 2.7182818284590452353602874713527…
- Φ (Golden Ratio) = 1.61803398874989484820…
- 22/7 = 3.1428571428571…
What are Imaginary Numbers?
Imaginary Numbers are numbers that cannot logically exist, where if we were to Square this number, we would end up with -1.
- √−1 = i
- i² = -1
- ∞ (infinity)
In mathematics, we show an Imaginary number using the symbol “i”.
Combining Real and Imaginary Numbers
Now the question remains, what happens when we combine a Real and Imaginary Number?
Well, this is known as a Complex Number.
For example:
Let’s say our Real Number is “5”, and our Imaginary Number is “i”. What happens when we add them together to try to work out what “z” is?
- z = 5 + i
Logically you cannot add these two numbers together, as it wouldn’t make any sense, because “i” has no defined value.
But what if we try to multiply “i” by a whole number of 3:
- z = 5 + 3i
This could work, so let’s graphically plot this on an X/Y Graph
- x = 5
- y = 3
Where “x” and “y” intersect is where we would visualize “z” on the Complex Plane.
Check out my tutorial on How to Use Python to start writing Python code right now
How Complex Numbers work in Python
Alright, now that we know a little bit more about Complex Numbers, let’s take a look at how to implement and use them in Python as mathematical formulas.
Python lets you use complex numbers in arithmetic expressions that calls a function, just as you would with other numbers in Python. It leads to elegant syntax that reads almost like a math textbook.
Complex Number Literals in Python
The quickest way to define a complex number in Python is to type its literal directly in the source code, just as we did in the above graph example, except we are replacing “i” with “j”:
z = 5 + 3j
While this might look like an algebraic formula, the expression to the right of the = is already a fixed value that requires no further evaluation.
We can confirm this by using the data type statement: “type()”
z = 5 + 3j
print(type(z))
#>> <class 'complex'>
We can confirm that it’s indeed a complex number.
If we remove the “j” from the statement, we see the Variable reverts back to a regular “int” Data Type:
z = 5 + 3
print(type(z))
#>> <class 'int'>
We can also use Floating point number in a Complex Number as well:
z = 5.43 + 3.75j
print(type(z))
#>> <class 'complex'>
In Python, complex number literals simply mimic the base Standard Form, Algebraic Form and even the Canonical Form.
Why “j” instead of “i” for Complex Numbers in Python?
Ok, so why does Python refer to Imaginary Numbers as “j” instead of “i”?
Well there are a few reasons for this:
- “j” is already a convention used by engineers to avoid name collisions with electric current, which is already referred to using “i”.
- “i” is often used for the indexing variable in loops in most coding languages.
- “i” can be easily confused with “l” or “1” in the source code.
You can either use lowercase “j” or uppercase “J” for an Imaginary Number in Python
Algebraic Form
The algebraic form of a complex number follows the same rules as standard algebra, making it very convenient to use in Python.
Addition
For example, addition algebra is commutative, meaning that you can swap the order of the equation around, without changing its value:
5 + 3j == 3j + 5
#>> True
Subtraction
As with standard subtraction algebra, you can also substitute addition for subtraction in a complex number:
5 - 3j == 5 + (-3j)
#>> True
Conclusion on Python Complex Numbers
I hope you enjoyed this tutorial on Complex Numbers in Python and that it helped you understand how they work and how to better use them.