Python complex() — A Useless Python Feature?

Rate this post

The Python complex() method returns a complex number object. You can either pass a string argument to convert the string to a complex number, or you provide the real and imaginary parts to create a new complex number from those.

This article shows you how to use Python’s built-in complex() constructor. You’ll not only learn how to use it—but also why it is useless and what you should do instead in newer Python versions.


Learn by example! Here are some examples of how to use the complex() built-in function:

>>> complex(1, -2)
>>> complex(2, -1)
>>> complex(2, 2)
>>> complex(1)
>>> complex(2)
>>> complex('42-21j')

Syntax Complex()

You can use the complex() method with three different argument lists.

complex(real)         # Imaginary Part is 0j
complex(real, img)    # Both real and imaginary part are given
complex(string)       # String has format 'x+yj' for real part x and imaginary part y. 
ArgumentsrealThe real part of the complex number
imgThe imaginary part of the complex number
stringA string defining the real and imaginary part in the form 'x+yj' or 'x+yJ' where x and y are integers for the real and imaginary parts.
Return ValuecomplexReturns a complex number.

Video Complex()

Python Complex() is Dead 😲 Long Live Python Complex()!

Interactive Shell Exercise: Understanding Complex()

Consider the following interactive code:

Exercise: Guess the output before running the code.

Check out my new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Publisher Link:

How to Create Complex Number Without complex()

Interestingly, you don’t need the complex() constructor to create a complex number! Instead, newer version of Python have built-in complex number support—just use the the syntax x+yj for real part x and imaginary part y to obtain a complex number.

a = 1+1j
b = 4+42j
c = 0+0j

print('Complex Numbers:')
print(a, b, c)

print(type(a), type(b), type(c))

The output is:

Complex Numbers:
(1+1j) (4+42j) 0j
<class 'complex'> <class 'complex'> <class 'complex'>


The Python complex() method returns a complex number object. To create a complex number:

  • Pass a string argument to convert the string to a complex number, or
  • Provide the real and imaginary parts to create a new complex number from those.

I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!