Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Complex Numbers

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.

00:22 Python is one of the few programming languages that provide native support for complex numbers. You can think of a complex number as a pair consisting of the real part in the imaginary part.

00:33 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.

00:46 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.

01:33 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.

01:51 For example, you can get the real and imaginary components of a complex number using its .real and .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.

02:08 You can also calculate a conjugate of a complex number by calling its .conjugate() method. A conjugate flips the sign of the imaginary unit.

02:18 In fact, all numbers in Python, including integers and floating-point numbers, have the .real and .imag attributes as well as the .conjugate() method.

02:26 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 0.

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,

02:58 multiply and divide them,

03:03 or raise one to a power, but you can’t use the floor division,

03:10 nor can you calculate the remainder of a division, which is only defined for whole numbers.

03:19 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.