Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A built-in numeric data type in Python used to represent complex numbers, consisting of a real part and an imaginary part. Both components are stored internally as 64-bit IEEE 754 double-precision floating-point numbers (float).

Instantiation

Complex numbers can be instantiated using literal syntax or the built-in complex() constructor. Python uses the suffix j or J to denote the imaginary unit (equivalent to i in mathematics).

# Literal syntax
c1 = 4 + 3j
c2 = -2.5 + 1j
c3 = 5j          # Real part implicitly 0.0


# Constructor syntax: complex(real, imag)
c4 = complex(4, 3)
c5 = complex(-2.5, 1)
c6 = complex(0, 5)


# String parsing via constructor (no spaces allowed around the operator)
c7 = complex("4+3j")

Attributes and Built-in Functions

The complex type exposes the real and imaginary components as read-only attributes, provides a built-in method for mathematical conjugation, and integrates with Python’s standard abs() function to calculate the modulus.
z = 3 + 4j


# Attributes return standard Python floats
real_part = z.real  # 3.0
imag_part = z.imag  # 4.0


# Returns a new complex object with the sign of the imaginary part reversed
conj = z.conjugate()  # (3-4j)


# The built-in abs() function returns the magnitude (modulus) as a float
magnitude = abs(z)  # 5.0

Arithmetic Operations

Complex numbers support standard arithmetic operations, which follow standard algebraic rules for complex arithmetic.
z1 = 2 + 3j
z2 = 1 - 1j

addition = z1 + z2       # (3+2j)
subtraction = z1 - z2    # (1+4j)
multiplication = z1 * z2 # (5+1j)
division = z1 / z2       # (-0.5+2.5j)
exponentiation = z1 ** 2 # (-5+12j)

Type Constraints and Exceptions

Because the complex plane lacks a natural linear ordering, complex objects do not support relational comparison operators. Furthermore, operations that imply a scalar continuum or remainder logic are explicitly disabled.
z1 = 2 + 3j
z2 = 4 + 1j


# Equality evaluation is supported
is_equal = (z1 == z2)  # False


# Relational comparisons raise TypeError

# z1 < z2  -> TypeError: '<' not supported between instances of 'complex' and 'complex'


# Floor division and modulo raise TypeError

# z1 // z2 -> TypeError: can't take floor of complex number.

# z1 % z2  -> TypeError: can't mod complex numbers.


# Explicit type casting to scalar types raises TypeError

# float(z1) -> TypeError: can't convert complex to float

# int(z1)   -> TypeError: can't convert complex to int

Standard Library Integration

While the built-in math module only supports scalar floats and will raise a TypeError if passed a complex number, Python provides the cmath module specifically for complex mathematical functions.
import cmath

z = 1 + 1j


# Calculate phase (argument)
phase = cmath.phase(z)  # 0.7853981633974483 (pi/4 radians)


# Convert to polar coordinates
polar = cmath.polar(z)  # Returns tuple: (1.4142135623730951, 0.7853981633974483)


# Convert from polar to rectangular
rect = cmath.rect(1.4142135623730951, 0.7853981633974483) # (1.0000000000000002+1j)
Master Python with Deep Grasping Methodology!Learn More