Lesson 05
complex() — Convert to Complex
Convert int, float, bool, and numeric strings with complex(). Use j, not i, and inspect .real and .imag.
complex() converts a value into a complex number.
Unlike int() and float(), Python represents complex numbers using j, not i.
What you'll learn
- Convert int, float, bool, and numeric strings
- Use
jas Python’s imaginary unit - Understand
.realand.imag - Recognize malformed complex strings
What does complex() do?
complex() converts a possible value into a complex object.
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.
print(complex(10))
print(complex(-12))(10+0j)
(-12+0j)Example 2 — Float → Complex
Floating-point numbers keep their decimal value.
print(complex(1.2))
print(complex(-1.3))(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) |
a = "12"
b = complex(a)
print(b)
print(b.real)
print(b.imag)(12+0j)
12.0
0.0Notice that .real and .imag are always stored as floats.
.real and .imag
Every complex number contains two parts.
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
complex("2+3j")Incorrect
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:
complex(-12)
complex("-23")Expected results: (-12+0j) and (-23+0j).
Exercise 2 — Print the two properties:
z = complex("12")
print(z.real)
print(z.imag)What data type are .real and .imag?
Exercise 3 — Uncomment this line:
complex("2-3i")Why does Python reject it?
Quick knowledge check
-
What is
complex(10)?
(10+0j) -
What is
complex(False)?
0j -
Why does
complex("2-3i")fail?
Python usesjas the imaginary unit, noti.
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, neveri, in complex strings. .realand.imaglet you inspect the two parts; both are stored as floats.
Loading playground…