To learn more about coding with complex numbers, check out Simplify Complex Numbers With Python.
00:00 In this lesson, you’ll go over some of the basics of working with complex numbers in Python. Because complex numbers are rarely used outside of the domains of scientific computing and computer graphics, they were only briefly mentioned before in this course. At the same time, feel free to skip this lesson if you have no interest in working with complex numbers.
There are two ways to create a complex number in Python. You can either call the built-in
complex() function with those two parts passed as arguments, or you can use a special syntax to define a complex-number literal.
Notice the letter
j next to the second number, which indicates the so-called imaginary unit. Mathematicians usually use the letter i to denote an imaginary unit, but Python follows a notation used by engineers to avoid confusing it for a lowercase letter l or a digit 1 that might appear in your code.
01:06 When you drop the imaginary unit, then your complex-number literal becomes just regular arithmetic expression, which adds two numbers. You can slap the imaginary unit to either of those two numbers in a complex-number literal to indicate that the imaginary part. When you only have the imaginary numbers in your literal, then Python will add them and create a complex number without the real part, but it’s still a complex number.
You would typically assign a complex number to a variable such as
z. Using the letter
z for a complex number is a historical convention dating back to Carl Friedrich Gauss. Just like integers and floating-point numbers, complex numbers have a few interesting attributes and methods in Python.
For example, you can get the real and imaginary components of a complex number using its
.imag attributes. Notice that the real and imaginary parts are stored as floating-point numbers even when you define a complex number in terms of integers.
That isn’t surprising from a mathematical standpoint because complex numbers are a superset of real numbers. For ins and floats, the
.real attribute and the
.conjugate() method will return the number itself, while the
.imag attribute will always be
02:44 Complex numbers support all the arithmetic operators that you learned about except for the integer division and the modulo operator. Otherwise, you can add two complex numbers, you can subtract them,
To sum up, you can define a complex number using either the built-in
complex() function or the complex-number literal with the letter
j glued to the imaginary part, You can also access your complex numbers’ attributes and methods to get the real part, the imaginary part, and to calculate the conjugate.
03:38 If you’d like to learn more about working with complex numbers and Python, then check out a dedicated tutorial entitled Simplify Complex Numbers With Python. Congratulations, you’ve made it to the end of this course. In the next and final lesson, you’ll get a quick summary of what you’ve learned in this course.
Become a Member to join the conversation.