Python complex()

The syntax of complex() is:

complex([real[, imag]])

complex() Parameters

In general, complex() method takes two parameters:

  • real - real part. If real is omitted, it defaults to 0.
  • imag - imaginary part. If imag is omitted, it defaults to 0.

If the first parameter passed to this method is a string, it will be interpreted as a complex number. In this case, the second parameter shouldn't be passed.


Return Value from complex()

As suggested by the name, complex() method returns a complex number.

If the string passed to this method is not a valid complex number, ValueError exception is raised.

Note: The string passed to complex() should be in the form real+imagj or real+imagJ


Example 1: How to create a complex number in Python?

z = complex(2, -3)
print(z)

z = complex(1)
print(z)

z = complex()
print(z)

z = complex('5-9j')
print(z)

Output

(2-3j)
(1+0j)
0j
(5-9j)

Example 2: Create complex Number Without Using complex()

It's possible to create a complex number without using complex() method. For that, you have to put 'j' or 'J' after a number.

a = 2+3j
print('a =',a)
print('Type of a is',type(a))

b = -2j
print('b =',b)
print('Type of b is',type(a))

c = 0j
print('c =',c)
print('Type of c is',type(c))

Output

a = (2+3j)
Type of a is <object>
b = (-0-2j)
Type of b is <object>
c = 0j
Type of c is <object>

Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges