HOWTO · Python

Imaginary Numbers in Python

This tutorial discusses imaginary numbers in Python

On this page

Python represents a value with real and imaginary components using its built-in complex type. Write the imaginary unit with j (or J), as in 8 + 5j, or construct a value with complex(real, imaginary). The examples below show how to create, inspect, calculate with, and use complex values in Python.

Represent Imaginary Numbers with Python Complex Numbers

A complex number has a real component and an imaginary component. Python uses j as the suffix for the imaginary component, following the engineering convention; for example, 8 + 5j represents 8 plus 5 times the imaginary unit. The built-in complex() constructor accepts the real and imaginary components as separate numbers. Both forms produce an instance of the complex type.

Run this example:

a = 8 + 5j
b = complex(8, 5)
print(type(a))
print(a == b)
<class 'complex'>
True

Use the constructor when your real and imaginary components are already separate values, such as values read from input or calculated elsewhere. Its second argument is the coefficient of the imaginary unit, so complex(8, 5) represents 8 + 5j.

Read the Parts and Conjugate of a Complex Number

The .real and .imag members are attributes, so read them without parentheses. They return the two components as floating-point values. The .conjugate() member is a method: call it with parentheses to change the sign of the imaginary component while keeping the real component the same.

For example, for 8 + 5j, the real part is 8.0, the imaginary part is 5.0, and the conjugate is (8-5j). The conjugate is useful in complex arithmetic, including when simplifying a quotient whose denominator is complex.

value = 8 + 5j
print(value.real)
print(value.imag)
print(value.conjugate())

Perform Arithmetic with Complex Numbers

Python’s arithmetic operators work directly on complex values. Addition combines the real components and the imaginary components independently; multiplication follows the usual algebraic rules and uses j * j == -1. For a = 8 + 5j and b = 10 + 2j, a + b is 18 + 7j, while a * b is 70 + 66j.

Adding only a.imag + b.imag gives 7.0; that expression adds the imaginary components alone and is not the sum of the two complex numbers. Use a + b when you intend full complex addition.

a = 8 + 5j
b = 10 + 2j
print(a + b)
print(a * b)

Use cmath for Complex-Valued Math

The standard-library cmath module provides mathematical functions that accept complex inputs and can return complex results. Use cmath.phase(value) for the angle in radians, cmath.exp(value) for the exponential, and functions such as cmath.sin(value) and cmath.sinh(value) for trigonometric and hyperbolic calculations. These are functions in the cmath module, not methods on the complex value.

import cmath

value = 1 + 1j
print(cmath.phase(value))
print(cmath.exp(value))
print(cmath.sin(value))
print(cmath.sinh(value))
0.7853981633974483
(1.4686939399158851+2.2873552871788423j)
(1.2984575814159773+0.6349639147847361j)
(0.6349639147847361+1.2984575814159773j)

The phase is measured in radians. The familiar real-valued math functions generally expect real inputs; use cmath when the calculation involves complex values. Results may display floating-point approximations, so compare computed values with a tolerance when using them in numerical checks.

Store Complex Numbers in NumPy Arrays

NumPy is an optional third-party library for working with arrays. Its numpy.array() function can store complex values in a one-dimensional array, for example numpy.array([8 + 5j, 10 + 2j, 4 + 3j]). NumPy infers a complex dtype for these values, allowing array operations to preserve both components. Install and import NumPy in your own environment before using this library example; Python’s built-in complex type does not require NumPy.

import numpy

values = numpy.array([8 + 5j, 10 + 2j, 4 + 3j])
print(values)
[ 8.+5.j 10.+2.j  4.+3.j]

Choose a NumPy array when you need to apply numerical operations to a collection of complex values. For a single value or a small calculation, the built-in type and operators are sufficient. Array display formatting is handled by NumPy, so the exact spacing and precision shown can depend on the values and print settings.