Back to Blog
Python

Python Complex Real Imag: Accessing Parts

python complex real imag: Learn how to access the real and imaginary parts of Python complex numbers using .real and .imag, and how to work with them effectively.

complex numbersPython numeric typesreal partimaginary partcmath
A diagram showing a complex number with real and imaginary axes, highlighting the .real and .imag attributes in Python.

Python's complex type stores numbers with a real and an imaginary component. The phrase python complex real imag refers to the two attributes you use to read those components: .real and .imag. This article explains how to access them, how they behave, and where they fit into real code.

The Complex Type and Its Attributes

In Python, a complex number is written as a + bj or complex(a, b). The j (or J) suffix denotes the imaginary unit. Internally, the value is stored as two floating-point numbers. The .real attribute returns the real part, and .imag returns the imaginary part. Both are read-only.

z = 3 + 4j print(z.real) # 3.0 print(z.imag) # 4.0

Notice that even if you construct the number with integers, .real and .imag are returned as float values. That is consistent with Python's numeric model: a complex number is always composed of two floats.

Reading and Writing Real and Imaginary Parts

Because .real and .imag are read-only, you cannot assign to them directly. To change a component, you must create a new complex number.

z = 3 + 4j z.real = 5 # AttributeError: attribute 'real' of 'complex' objects is not writable

Instead, construct a new value:

z = 3 + 4j z = 5 + z.imag * 1j # new complex with real=5, imag=4

This immutability is consistent with other numeric types in Python and avoids surprising aliasing issues.

Using complex() to Build Numbers

The complex() constructor accepts two arguments, both optional. If you pass a single number, it becomes the real part and the imaginary part defaults to zero. If you pass two numbers, they become the real and imaginary parts respectively.

a = complex(2, 3) # (2+3j) b = complex(2) # (2+0j) c = complex() # (0+0j)

You can also pass a string representation, but that is less common and has strict formatting rules.

Arithmetic with Complex Numbers

The standard arithmetic operators work with complex numbers. Addition, subtraction, multiplication, and division follow the usual rules. The .real and .imag attributes are useful when you need to extract components for further processing, such as plotting or formatting.

z1 = 1 + 2j z2 = 3 - 4j z_sum = z1 + z2 # (4-2j) print(z_sum.real, z_sum.imag) # 4.0 -2.0

When you need the magnitude or phase, the cmath module provides functions like abs() and phase(). These internally use the real and imaginary parts, but you rarely need to access them manually for such calculations.

Handling Edge Cases in Complex Math

Complex numbers can produce surprising results when combined with functions that expect real inputs. For example, math.sqrt(-1) raises a ValueError, but cmath.sqrt(-1) returns 1j. Knowing how to access .real and .imag helps you separate the components when you need to pass them to real-only functions.

Another edge case is floating-point precision. Because .real and .imag are floats, operations like 0.1 + 0.2j will have the same precision issues as any float arithmetic. When comparing complex numbers, use a tolerance rather than exact equality.

Performance and Memory Considerations

Complex numbers in Python are objects, but they are implemented in C and are quite efficient for arithmetic. Accessing .real and .imag is a simple attribute lookup, so it is cheap. However, creating many complex numbers in a tight loop can still incur allocation overhead. If you are processing large arrays of complex data, consider using NumPy's complex arrays, which store the data contiguously and offer vectorized operations.

In pure Python, a complex number occupies more memory than a float because it stores two floats. This is rarely a problem unless you are storing millions of them.

When to Use Complex Numbers in Production Code

Complex numbers are natural for signal processing, control systems, and scientific computing. In web or application code, they appear less often, but they can be useful for representing two-dimensional coordinates or rotations. The .real and .imag attributes give you a clean way to extract the components when you need to interface with other systems that expect separate values.

For example, when serializing a complex number to JSON, you might convert it to a dictionary with real and imag keys:

z = 3 + 4j payload = {"real": z.real, "imag": z.imag}

This is a common pattern in APIs that need to transmit complex data.

python complex real imag: Practical Usage and Code Examples | RYUSLOG DEV