Python Reference Library →

Python complex() Function – Create Complex Numbers Easily

Let’s look at complex() function – create complex numbers easily

  1. Notice

    complex() creates a number with real and imaginary parts. The imaginary component is written with j in Python.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
number = complex(3, 2)
print(number)
print(number.real)
Live state

Top-level scope

number→Object 1
Object 1 · complex(3+2j)
This step

complex() creates a number with real and imaginary parts. The imaginary component is written with j in Python.

Picture complex() function – create complex numbers easily

complex() creates a number with real and imaginary parts. The imaginary component is written with j in Python.

Image brief

Alt text
A diagram for Python complex() Function – Create Complex Numbers Easily. Instructions: number = complex(3, 2) print(number) print(number.real). Displayed result: (3+2j) 3.0. The final example bindings are number --> object 1 (complex). object 1: (3+2j)
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python complex() Function – Create Complex Numbers Easily". Teaching focus: complex() creates a number with real and imaginary parts. The imaginary component is written with j in Python. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS number = complex(3, 2) print(number) print(number.real) | v DISPLAYED RESULT (3+2j) 3.0 FINAL BINDINGS number --> object 1 (complex) object 1: (3+2j) Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python complex() Function – Create Complex Numbers Easily. Instructions: number = complex(3, 2) print(number) print(number.real). Displayed result: (3+2j) 3.0. The final example bindings are number --> object 1 (complex). object 1: (3+2j)

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
number = complex(3, 2)
print(number)
print(number.real)
Live state

Top-level scope

number→Object 1
Object 1 · complex(3+2j)
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
(3+2j)
3.0

What do you expect this program to print?

What does this example display?

number = complex(3, 2)
print(number)
print(number.real)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

(3+2j) 3.0

Follow the printed values in order. The output is: (3+2j) 3.0

More examples and reference

Python complex() Function

The complex() function in Python is used to create complex numbers. A complex number has a real part and an imaginary part, like 3 + 4j.

This function is useful in math, simulations, electrical engineering, and many scientific applications.

Syntax

complex(real=0, imag=0)

Parameters:

  • real (optional) – The real part (default is 0)
  • imag (optional) – The imaginary part (default is 0)

Returns:

  • A complex number object: real + imag*j

Example 1: Real and Imaginary Parts

z = complex(3, 4)
print(z)
(3+4j)

Example 2: Only Real Part

z = complex(5)
print(z)
(5+0j)

Example 3: Using a String

z = complex("2+3j")
print(z)
(2+3j)

Note: If you use a string, it must be a valid complex number format.

Real-World Use Cases

  • Electrical engineering: representing impedance or current with imaginary components
  • Signal processing and physics simulations
  • Mathematics involving imaginary numbers

Accessing Real and Imaginary Parts

z = complex(3, 4)
print(z.real)   # Output: 3.0
print(z.imag)   # Output: 4.0

Common Mistakes

  • Passing a string with space: complex("2 + 3j") will raise a ValueError.
  • You can’t pass both a string and a second argument: complex("3+4j", 2) is invalid.

Interview Tip

Complex numbers are rarely asked directly in interviews, but you may encounter them in signal processing or competitive math problems. Know how to extract .real and .imag values.

Summary

  • complex() creates a complex number.
  • You can pass real and imaginary parts as numbers or a single valid string.
  • Access components with .real and .imag.

Practice Problem

Write a program that reads two numbers from the user and creates a complex number:

real = float(input("Enter real part: "))
imag = float(input("Enter imaginary part: "))
z = complex(real, imag)
print("The complex number is:", z)

What to remember

Connect what you noticed in the example with the idea below.

Remember

complex() creates a number with real and imaginary parts. The imaginary component is written with j in Python.

Watch out for this idea

The second argument to complex() is another real component.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →