DEV Community

Cover image for Quark’s Outlines: Python Complex Numbers
Mike Vincent
Mike Vincent

Posted on

Quark’s Outlines: Python Complex Numbers

Overview, Historical Timeline, Problems & Solutions

An Overview of Python Complex Numbers

What is a Python complex number?

You may want to work with numbers that have both a real and an imaginary part. In Python, you use a complex number to do this. A Python complex number holds two values: one for the real part and one for the imaginary part. It uses j to show the imaginary part.

Python complex numbers follow this form: a + bj, where a is the real part and b is the imaginary part. Both a and b are floating point numbers. You can write 3 + 4j, and Python understands it as a complex number.

Python lets you create complex numbers using j syntax.

z = 3 + 4j
print(z)          # (3+4j)
print(z.real)     # 3.0
print(z.imag)     # 4.0
Enter fullscreen mode Exit fullscreen mode

When you write a complex number, Python gives you access to the .real and .imag attributes. These show the real and imaginary parts as float values.

How do Python complex numbers behave in math?

Python complex numbers support addition, subtraction, multiplication, and division. You can also find the absolute value of a complex number using abs(). Python follows math rules for complex numbers. This makes it easy to use them in scientific and engineering code.

You can do math between complex numbers or between complex and real numbers. Python does the type matching and gives you the correct result.

Python lets you perform math using complex numbers.

a = 2 + 3j
b = 1 - 1j

print(a + b)    # (3+2j)
print(a * b)    # (5+1j)
print(abs(a))   # 3.605551275463989
Enter fullscreen mode Exit fullscreen mode

When you use abs() on a complex number, Python returns the distance from the origin in the complex plane. This is also called the modulus or magnitude.


A Historical Timeline of Python Complex Numbers

Where do Python’s complex number rules come from?

Python complex numbers reflect math ideas that go back hundreds of years. These rules were added to support scientific work, circuit modeling, and math research. Python includes built-in support to avoid requiring extra libraries for basic complex math.


People invented ways to write imaginary numbers.

1572 —Imaginary number notation Rafael Bombelli introduced the idea of square roots of negative numbers using √-1.

1806 —The symbol i for imaginary Jean-Robert Argand popularized the use of i to represent imaginary values.

People designed Python to support complex numbers.

1991 —Built-in complex number type Python 0.9.0 included complex numbers as a native type using j for the imaginary part.

2001 —.real and .imag attributes Python 2.2 gave easy access to the parts of a complex number.

2011 —cmath module extended Python 3.2 improved math for complex numbers with logarithms and trigonometric functions.

People kept Python complex numbers simple and stable.

2023 —No changes to syntax or structure Complex numbers remain stable and well-supported.

2025 —Python core team supports built-in math The format a + bj remains the preferred way to write complex values.


Problems & Solutions with Python Complex Numbers

How do you use Python complex numbers the right way?

Python complex numbers help when you need both real and imaginary values. You can model waves, signals, or rotations. The problems below show how to write, inspect, and work with Python complex values in code.


Problem: How do you store a number with a real and imaginary part in Python?

You are modeling a circuit or signal. The result has both a real part and an imaginary part. You need a way to store both parts in one value.

Problem: You try using a tuple like (3, 4) or two separate variables, but that makes math harder.

Solution: Use a Python complex number using the j suffix to store both parts together.

Python lets you store real and imaginary parts using complex syntax.

z = 3 + 4j
print(z)        # (3+4j)
print(z.real)   # 3.0
print(z.imag)   # 4.0
Enter fullscreen mode Exit fullscreen mode

This gives you one object with two float parts. You can access each part using .real and .imag.


Problem: How do you do math with imaginary values in Python?

You are solving a problem that includes imaginary units. You need to add, subtract, and multiply complex values.

Problem: You try using functions, but the math is slow and complex.

Solution: Use Python complex numbers directly and let Python handle the math rules.

Python lets you perform arithmetic with complex numbers.

a = 1 + 2j
b = 2 - 1j

print(a + b)    # (3+1j)
print(a * b)    # (4+3j)
print(a - b)    # (-1+3j)
Enter fullscreen mode Exit fullscreen mode

Python follows the standard math for complex numbers. You do not need to write the formulas yourself.


Problem: How do you check the size of a complex value in Python?

You are working with signals. You want to measure the strength of a signal as a single number, even if the value is complex.

Problem: You print the number and see two parts, but you want just the distance from zero.

Solution: Use abs() on a Python complex number to get its magnitude.

Python lets you find the length of a complex number using abs().

z = 3 + 4j
print(abs(z))   # 5.0
Enter fullscreen mode Exit fullscreen mode

This gives the length from (0, 0) to (3, 4). It uses the Pythagorean formula under the hood.


Problem: How do you combine real and complex values in Python?

You are writing a formula where a real value and a complex value appear together. You want to mix them in one expression.

Problem: You are not sure if adding 5 and 2j is legal in Python.

Solution: Python lets you combine real and complex numbers. The result is always complex.

Python lets you mix real and complex values in one expression.

x = 5
y = 2j
z = x + y

print(z)        # (5+2j)
print(type(z))  # <class 'complex'>
Enter fullscreen mode Exit fullscreen mode

Python converts the result to a complex number automatically.


Problem: How do you store and inspect complex data in Python?

You are building a signal processing tool. You want to store many complex numbers and inspect their values later.

Problem: You are not sure how to loop over them or get their parts.

Solution: Use a Python list of complex numbers and inspect each one with .real and .imag.

Python lets you work with complex numbers inside lists.

data = [1+2j, 3+4j, 5-1j]

for z in data:
    print(f"real = {z.real}, imag = {z.imag}")
Enter fullscreen mode Exit fullscreen mode

This prints each part on its own line. You can use this in real applications or tests.


Like, Comment, Share, and Subscribe

Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!


Mike Vincent is an American software engineer and app developer from Los Angeles, California. More about Mike Vincent

Top comments (0)