Complex Numbers in Python: A Practical Guide

When you use a complex number in a Python calculation, you need to know how to represent it and work with its two components. Python’s built-in complex type stores those components together, with j marking the imaginary unit.

This guide shows how to calculate with complex values and convert them between rectangular and polar form.

TL;DR

Python has a built-in complex type for values with horizontal and vertical components. Inspect them with the component attributes and, and use cmath for complex math. Choose a sorting key and round each component separately.
  • Create a value with \1 or \1.
  • Use \1 for magnitude and \1 for angle in radians.
  • Convert between rectangular and polar forms with \1 and \1.

The bullets above give the short answer. The definition below explains the two-part value Python stores.

What Are Complex Numbers in Python?

A complex number combines two coordinates, often written as a + bi. Python uses j for the imaginary unit, so 3 + 4j has coordinates 3 and 4.

A complex number pairs two coordinates under one numeric type. This section applies that representation to calculations and conversions.

How to Work With Complex Numbers Step by Step

A complex value stores two numeric components, and Python exposes operations for arithmetic, magnitude, and coordinate conversion.

Start by constructing one value and inspecting the coordinates Python exposes.

Step 1: Create a value and read its components

Use a literal such as 3 + 4j, or call complex(horizontal, vertical). You can also pass a string such as “3+4j” to the constructor. For z = 3 + 4j, read its components with its component attributes, which return 3.0 and 4.0. The conjugate, z.conjugate(), changes the sign of the imaginary component, producing (3-4j).

Once the value exists, arithmetic and magnitude answer common calculation questions.

Step 2: Calculate with operators and magnitude

The familiar operators handle addition, subtraction, multiplication, division, and exponentiation. Use abs(z) for magnitude, the distance from zero. Addition and subtraction combine matching components. For example, (a + bj) + (c + dj) gives (a + c) + (b + d)j, while subtraction uses the difference of each pair. For two points, abs(z1 – z2) gives their separation. This is useful when complex values represent positions or signal samples and a calculation needs their difference. Multiplication follows component arithmetic. For (a + bj) * (c + dj), the result is (a*c – b*d) + (a*d + b*c)j. This rule comes from expanding the product and replacing j squared with negative one. Polar multiplication combines magnitudes and adds phase angles. A unit-magnitude factor rotates the value while preserving its distance from zero. Division by (c + dj) can use its conjugate, making the denominator c*c + d*d. The divisor must be nonzero. For a collection, choose a sorting rule first. Use sorted(values, key=abs) to sort by magnitude, or order by coordinates with a tuple key.

For angle-based work, convert the coordinates to magnitude and phase, then rebuild them when needed.

Step 3: Convert between rectangular and polar form

Polar form stores magnitude r and phase phi. The cmath.polar function returns them, while cmath.rect rebuilds a value from those coordinates. Use ** for integer exponents, and use cmath.log or cmath.log10 when a logarithm is part of the calculation. These functions return complex values, including when the imaginary coordinate is zero. For formulas that need mathematical constants, cmath.pi and cmath.tau express angles, while cmath.e is the exponential constant. Functions in cmath accept integer, floating-point, and complex arguments. They can also accept objects that define __complex__() or __float__(), which lets custom numeric types participate in these calculations. Classification functions check exceptional values. cmath.isfinite(z) checks both components, while cmath.isinf(z) checks either component for infinity and cmath.isnan(z) detects not-a-number values. Use cmath.isclose(a, b) for approximate comparison, with a positive abs_tol when comparing with zero.
Complex number represented as a vector on horizontal and vertical axes
A complex value maps to a point on the plane: horizontal component on the horizontal axis, imaginary part on the vertical axis.

The complete script puts construction, measurement, conversion, rounding, and ordering together.

Step 4: Inspect a complete example

The following command checks each operation on one value. Its output includes magnitude, phase, conversion, component rounding, and sorting.
Terminal running python3 complex_numbers_demo.py and showing complex-number components, magnitude, phase, conversions, rounding, and sorting
Output from \1.
The exact output is:
value: (3+4j)
real: 3.0
imaginary: 4.0
magnitude: 5.0
phase radians: 0.9272952180016122
polar: (5.0, 0.9272952180016122)
back to rectangular: (3.0000000000000004+3.9999999999999996j)
rounded components: 3.0 4.0
sorted by magnitude: [(1+1j), (-2+0j), (3+4j)]
Use cmath.phase(z) to find the phase angle. A one-coordinate ratio can divide by zero and miss a quadrant. cmath.phase() handles both coordinates and returns radians.

The example covers the normal path. The table below handles operations that need an explicit choice or raise an error.

Limits and Common Complex-Number Questions

A few familiar operations on ordinary numbers need a more explicit choice for complex values. Knowing the distinction avoids errors and misleading output.
CaseSymptomFix
Round a complex value\1 raises \1.Use \1 when both coordinates need rounding.
Compare complex values\1 raises \1.Use \1 for greatest magnitude, or choose another explicit key.
Choose a math module\1 raises \1.Use \1 to get \1.
Rounding each coordinate is a deliberate choice. For functions that may return complex results, choose cmath. Python 3.14 documentation deprecates the two-argument form only when both inputs are complex values. Keep ordinary constructor calls such as complex(3, 4) for numeric components.

These cases show why complex-number operations sometimes need a stated rule rather than an implicit one.

Conclusion

Use the Python documentation for numeric types, complex(), and cmath when you need the exact behavior. AskPython’s guides to Python built-in functions and exponentiation cover related operations.

Use these answers for the common construction, arithmetic, rounding, and angle questions.

FAQ

How do you represent a complex number in Python?

Use a literal such as 3 + 4j or call complex(3, 4). Python uses j for the imaginary unit.

How do you add complex numbers in Python?

Use the + operator, for example (1 + 2j) + (3 + 4j), which evaluates to (4+6j).

How do you round a complex number in Python?

Round its components separately, for example complex(round(z.real, 2), round(z.imag, 2)). round(z) does not accept a complex argument.

How do you get the angle of a complex number?

Use cmath.phase(z). It returns the phase angle in radians and handles quadrants correctly.

Vijaykrishna Ram
Vijaykrishna Ram
Articles: 99