Python - Complex Numbers

6/9/2026

Complex numbers are a built-in numeric data type in Python used to represent values that consist of a real part and an imaginary part. It is made of two distinct parts:

  1. Real Part: A standard integer or floating-point number.
  2. Imaginary Part: A number followed by the letter j or J.

Defining a Complex Number

To creat a complex number the real part and imaginary part are combined using the + or - operator. The syntax is represented as a + bj.

python

How to use the "j"

In mathematics, the imaginary unit is often represented by ii. However, in Python, the suffix j or J is mandatory.

  • Correct: z = 2 + 4j
  • Incorrect: z = 2 + 4i (This will result in a SyntaxError)

The Coefficient

The imaginary part must consist a coefficient, this mean that we cannot simply write j or J alone, it must be represented with a coefficient even if the coefficient is 1, it must be explicitly written as 1j.

  • Correct: z = 1j
  • Incorrect: z = j (This will result in a NameError)

The complex() Constructor

There is a built-in function called complex() in python used to initailize complex numbers. This is useful when creating numbers from variables or when the real and imaginary parts are determined dynamically complex(real, imaginary).

python

Here are some behaviors to look out for:

  • If both arguments are omitted, the function returns 0j.
  • The arguments can be integers or floats, but the resulting real and imaginary components are always stored as floats.

Accessing Attributes

Once a complex number is defined, its individual components can be retrieved using two built-in attributes: .real and .imag.

Attributes:

  1. .real: Returns the real component of the complex number as a float.
  2. .imag: Returns the imaginary component of the complex number as a float.

python

The conjugate() Method

The .conjugate() method is a built-in function that returns a new complex number with the sign of the imaginary part reversed.

Mathematical Representation:
If a complex number zz is defined as a+bja + bj, the conjugate zˉ\bar{z} is defined as:

zˉ=abj\bar{z} = a - bj

python

Operations and Constraints

Complex numbers in Python support standard arithmetic operations but are restricted by certain mathematical constraints.

Basic Arithmetic

You can perform addition, subtraction, multiplication, and division between complex numbers. Python handles these operations according to the laws of complex algebra.

Operations Table:

OperationSyntaxLogic
Additionz1 + z2(a+c)+(b+d)j(a+c)+(b+d)j
Subtractionz1 - z2(a−c)+(b−d)j(a−c)+(b−d)j
Multiplicationz1 * z2(ac−bd)+(ad+bc)j(ac−bd)+(ad+bc)j
Divisionz1 / z2Calculated via the conjugate of the denominator

python

Comparison Constraints

Unlike integers and floats, complex numbers do not have a natural order. Consequently, they cannot be compared using magnitude-based operators.

Constraint:
In Python, the following operators will raise a TypeError when used with complex numbers:

  • Less than (<)
  • Greater than (>)
  • Less than or equal to (<=)
  • Greater than or equal to (>=)

Equality Check:

Only the equality (==) and inequality (!=) operators are valid for complex numbers. Two complex numbers are equal only if their real parts and imaginary parts are both identical.

python