 # Python complex()

The python complex function converts the strings or numbers into complex integers. If you look at the syntax given below, you can see that the complex function takes two optional parameters, i.e., first, and real. Source Code

The syntax for the python complex function is as follows:

complex([real[, imag]])

### complex() Parameters

The two parameters of the complex function are:

1. real- this indicates the real part of the complex number. If the real value is not given, by default the value is zero.
2. imag- this indicates the real part of the complex number. If the real value is not given, by default the value is zero.

The first argument passed to this function will be treated as a complex number if it is a string. The second parameter should not be given in this scenario.

### Return Value from complex()

The complex() will return a complex number when the function is called. If the syntax of the complex() is not followed, and the number is not a valid complex number then a ValueError exception is thrown.

If the user is passing a string to the complex() it should be in the form of real+imagj or real+imagJ.

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

To create a complex number in python, we can use the complex() built-in function.

Source Code:

                    
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()

                    
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

b = (-0-2j)

Type of b is

c = 0j

Type of c is




### Example 3: This example allows strings to be used as parameters

                    
a = complex('1') # Passing single parameter

b = complex('1.5')

# Displaying the result

print(a)

print(b)

Output:

(1+0j)

(1.5+0j)



Example 4: the complex() accepts only one string parameter as the input. If the first parameter is a string, then they should not pass any other argument. If a second argument is passed, then a TypeError exception is raised.

                    
a = complex('1','2') # Passing two parameter

# Displaying the result

print(a)

Output:

TypeError: complex() can't take the second arg if first is a string



Example 5:

The complex method allows the complex number to be passed as the argument. Here’s an example for the same:

                    
a = complex(1+2j) # Passing a single parameter

b = complex(1+2j,2+3j) # Passing both of the parameters

# Displaying the result

print(a)

print(b)

Output:

(1+2j)

(-2+4j)



## How do you write complex in Python?

A complex number is of the format “a+bi.” Where a is the real number and b is the imaginary number. The complex() converts a,b into the complex integers. The real parts of the complex number can be accessed using the real function, and the imaginary part is accessed by the imag().

For example:

                    
import cmath

# Initializing real numbers

a = 5

b = 3

# we are converting a and b into complex numbers

c = complex(a,b);

# A complex number's real and imaginary parts are printed.

print ("The complex number's real end is ",end="")

print (c.real)

print ("The complex number's imaginary end is : ",end="")

print (c.imag)

Output:

The real part of the complex number is: 5.0

The imaginary part of the complex number is: 3.0



Program to show the demonstration of complex numbers

In this program, we are passing the integers as the arguments. These arguments are taken as real and imaginary numbers.

Source Code

                    
x = complex(1) # Passing single parameter

y = complex(1,2) # Passing both parameters

# Displaying result

print(x)

print(y)

Output:

(1+0j)

(1+2j)



We can also pass the floating-point values as the arguments

## How do you take complex input in python?

To take the complex numbers as input in python, the users will have to use the input() followed by the complex() to convert the input into complex numbers. The complex() is used to separate the real values from the imaginary values:

For example:

                    
compx = complex(input());

print(compx.real, compx.imag);

output:-

>>> complx = complex(input());

1+2j

>>> print(complx.real, complx.imag);

1.0 2.0



Share with friends

## Customize your course in 30 seconds

##### Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.  Ashhar Firdausi
IIT Roorkee
Biology  Dr. Nazma Shaik
VTU
Chemistry  Gaurav Tiwari
APJAKTU
Physics
Get Started

## Browse

##### Methods and Functions Subscribe
Notify of