Skip to main content

Python Complex Data Type

1. What is a Complex Data Type?​

At first glance, the word complex may sound intimidating, but in reality, it simply refers to a type of number system that you might have encountered during your school days in topics like algebra or calculus. A complex number is generally written in the form:

a + bj

Where:

  • a → Real part
  • b → Imaginary part
  • j → Imaginary unit (√-1)

In mathematics, you might have seen the symbol i used for √-1, but in Python, we must use j instead of i, and this is not optional—it is mandatory.

2. Why Does Python Support Complex Numbers?​

Unlike many programming languages such as C, C++, or even Java (in their basic forms), Python provides direct support for complex numbers.

This is particularly useful in domains such as:

  • Scientific computing
  • Electrical engineering
  • Signal processing
  • Mathematical modeling

Because these domains frequently involve calculations that require imaginary numbers, Python simplifies the process by offering a built-in complex type.

3. Creating Complex Numbers in Python​

Let us start with a simple example:

x = 10 + 20j
print(type(x)) # <class 'complex'>

This confirms that Python recognizes the value as a complex number.

Important Rules While Creating Complex Numbers​

  • You must use j to represent the imaginary part.
  • You can use either lowercase j or uppercase J.
x = 10 + 20j
y = 10 + 20J

4. Accessing Real and Imaginary Parts​

Every complex number in Python provides two built-in attributes:

  • .real → Returns the real part
  • .imag → Returns the imaginary part
x = 10 + 20j

print(x.real) # 10.0
print(x.imag) # 20.0

An interesting observation here is that Python always returns these values as floats, even if you originally provided integers.

5. Data Types of Real and Imaginary Parts​

Both parts of a complex number can be either:

  • Integer values
  • Floating-point values
x = 10 + 20j # Both integers
y = 10.5 + 20j # Real part is float
z = 10.5 + 20.6j # Both are floats

All of the above are valid and will be treated as complex numbers.

6. Important Rule About Number Bases​

This is a subtle but very important concept.

The real part can be written in different bases:

x = 0b1111 + 20j # Binary (15 + 20j)
x = 0o17 + 20j # Octal
x = 0xF + 20j # Hexadecimal

The imaginary part, however, must always be in decimal form.

x = 15 + 0b1111j # Invalid

This will result in a syntax error, because Python does not allow binary, octal, or hexadecimal representations for the imaginary component.

7. Arithmetic Operations on Complex Numbers​

One of the most powerful features of Python’s complex data type is that it allows you to perform mathematical operations directly.

x = 10 + 20j
y = 20 + 30j

print(x + y) # Addition
print(x - y) # Subtraction
print(x * y) # Multiplication
print(x / y) # Division

Output

(30+50j)
(-10-10j)
(-400+700j)
(0.615...+0.076...j)

You do not need to manually apply formulas for real and imaginary parts—Python handles all the internal calculations automatically.

  1. Behind the Scenes Even though Python simplifies everything, internally:
  • Addition and subtraction are performed component-wise
  • Multiplication follows algebraic rules:
(a + bj)(c + dj) = (ac - bd) + (ad + bc)j

However, as a developer, you usually do not need to worry about these details unless you are working on mathematical algorithms.