complex()

complex(real=0, imag=0)
Returns: complex · Added in v3.0 · Updated March 13, 2026 · Built-in Functions
built-in numbers complex math

The complex() function returns a complex number with real and imaginary parts. Complex numbers are numbers that have both a real part and an imaginary part, written as a + bj where a is the real part and b is the imaginary part (in Python, j is used instead of i to represent the imaginary unit).

Syntax

complex(real=0, imag=0)

Parameters

ParameterTypeDefaultDescription
realint, float, or str0The real part of the complex number, or a string representing a complex number
imagint or float0The imaginary part of the complex number (ignored if real is a string)

Examples

Basic usage

# Create a complex number with real and imaginary parts
z = complex(3, 4)
print(z)
# (3+4j)

# Using default values
z = complex()
print(z)
# 0j

Creating complex numbers with different types

# From integers
z = complex(5, 2)
print(z)
# (5+2j)

# From floats
z = complex(1.5, -2.5)
print(z)
# (1.5-2.5j)

# Mixed types
z = complex(3, 2.5)
print(z)
# (3+2.5j)

Converting from strings

# String must contain no spaces (Python 3)
z = complex("3+4j")
print(z)
# (3+4j)

# Negative imaginary part
z = complex("1-2j")
print(z)
# (1-2j)

# Just real part as string
z = complex("5")
print(z)
# (5+0j)

Working with complex numbers in Python

# Access real and imaginary parts
z = complex(3, 4)
print(z.real)  # 3.0
print(z.imag)  # 4.0

# Complex conjugate
print(z.conjugate())  # (3-4j)

# Absolute value (magnitude)
print(abs(z))  # 5.0 (which is sqrt(3^2 + 4^2))

Common Patterns

Mathematical operations with complex numbers

# Addition
z1 = complex(1, 2)
z2 = complex(3, 4)
print(z1 + z2)  # (4+6j)

# Multiplication
print(z1 * z2)  # (-5+10j)

# Division
print(z1 / z2)  # (0.44+0.08j)

# Powers
print(complex(2, 1) ** 2)  # (3+4j)

Using the cmath module for complex math functions

import cmath

z = complex(1, 1)
print(cmath.sin(z))   # (1.2984576+0.6349639j)
print(cmath.cos(z))   # (0.8337300-0.9888977j)
print(cmath.exp(z))   # (1.4686939+2.2873552j)
print(cmath.log(z))   # (0.3465736+0.7853982j)

Errors

Invalid string format

# Spaces in string cause ValueError
try:
    z = complex("3 + 4j")
except ValueError as e:
    print(e)
# complex() arg is a malformed string

Mixing string with imaginary parameter

# Cannot provide string and imag parameter together
try:
    z = complex("3+4j", 2)
except TypeError as e:
    print(e)
# complex() can't take a third argument

See Also