Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 05

complex() — Convert to Complex

Convert int, float, bool, and numeric strings with complex(). Use j, not i, and inspect .real and .imag.

Beginner18 min

complex() converts a value into a complex number.

Unlike int() and float(), Python represents complex numbers using j, not i.

complex(10) becomes (10+0j)

What you'll learn

  • Convert int, float, bool, and numeric strings
  • Use j as Python’s imaginary unit
  • Understand .real and .imag
  • Recognize malformed complex strings

What does complex() do?

complex() converts a possible value into a complex object.

python
number = complex(value)

If the original value has no imaginary part, Python automatically adds 0j.

Example 1 — Integer → Complex

Every integer becomes a complex number with an imaginary part of 0.

python
print(complex(10))
print(complex(-12))
text
(10+0j)
(-12+0j)

Example 2 — Float → Complex

Floating-point numbers keep their decimal value.

python
print(complex(1.2))
print(complex(-1.3))
text
(1.2+0j)
(-1.3+0j)

Example 3 — Boolean → Complex

Booleans convert just like numbers.

Boolean complex()
True (1+0j)
False 0j

False becomes the zero complex number, displayed as 0j.

Example 4 — String → Complex

complex() accepts three numeric string formats:

String Result
"12" (12+0j)
"1.2" (1.2+0j)
"2+3j" (2+3j)
python
a = "12"
b = complex(a)

print(b)
print(b.real)
print(b.imag)
text
(12+0j)
12.0
0.0

Notice that .real and .imag are always stored as floats.

.real and .imag

Every complex number contains two parts.

Real part 2 and imaginary part 3

For (2+3j):

Property Value
.real 2.0
.imag 3.0

Invalid string examples

These strings are malformed, so Python raises ValueError.

Input Why it fails
"True" Not a numeric literal
"2-3i" Python uses j, not i
"PYTHON" Not a number

Correct

python
complex("2+3j")

Incorrect

python
complex("2-3i")

Interactive playground

Try different source types and inspect the real and imaginary parts.

Choose the source type

Python simulation

complex(10)

Conversion successful

(10+0j) complex

.real
10.0
.imag
0.0

complex conversion playground

Complete conversion map

Source Example Result
int complex(10) (10+0j)
float complex(1.2) (1.2+0j)
bool complex(True) (1+0j)
bool complex(False) 0j
Integer string complex("12") (12+0j)
Float string complex("1.2") (1.2+0j)
Complex string complex("2+3j") (2+3j)
"True" complex("True") ValueError
"2-3i" complex("2-3i") ValueError

Practice exercises

Exercise 1 — Convert these values:

python
complex(-12)
complex("-23")

Expected results: (-12+0j) and (-23+0j).

Exercise 2 — Print the two properties:

python
z = complex("12")

print(z.real)
print(z.imag)

What data type are .real and .imag?

Exercise 3 — Uncomment this line:

python
complex("2-3i")

Why does Python reject it?

Quick knowledge check

  1. What is complex(10)?
    (10+0j)

  2. What is complex(False)?
    0j

  3. Why does complex("2-3i") fail?
    Python uses j as the imaginary unit, not i.

Summary

  • complex() converts integers, floats, booleans, and valid numeric strings into complex numbers.
  • Real-only values automatically receive an imaginary part of 0j.
  • Use j, never i, in complex strings.
  • .real and .imag let you inspect the two parts; both are stored as floats.

Loading playground…