AppDividend
Latest Code Tutorials

# Python complex() Example | complex() Function In Python

Python complex() Example | complex() Function In Python is today’s topic. Python complex function returns a complex number by specifying a real number and an imaginary number. It returns a complex number when real and imaginary parts are provided, or it converts a string to a complex number.

## #What is Complex Number

The complex number is represented by the “ x + yi“.

Python converts the real numbers x and y into the complex using the function complex(x,y). The real part can be accessed using a function real(), and the imaginary part can be represented by imag().

Geometrically, the phase of the complex number is an angle between the positive real axis and a vector representing the complex number. This is also known as the argument of the complex number.

The phase is returned using phase(), which takes the complex number as an argument.

The range of phase lies from -pi to +pi. i.e from -3.14 to +3.14.

## Python complex() Example

It converts a string or a number into a complex number. See the following syntax.

`complex(real, imag)`

In general, a complex() method takes two parameters:

1. real – Real part of the complex number. If real is omitted, by default its value is 0.
2. imag – Imaginary part of the complex number. If imag is omitted, by default its value is 0.

See the following code example.

```# app.py

data = complex(11, 21)
print(data)```

See the following output.

```➜  pyt python3 app.py
(11+21j)
➜  pyt```

Okay, Convert a string into a complex number.

```data = complex('19+21j')
print(data)```

See the following output.

```➜  pyt python3 app.py
(19+21j)
➜  pyt```

It allows only one string parameter. If the first argument is a string type, it does not allow to pass the second argument. It generates an error if we pass the second parameter.

See the following code example.

```# app.py

data = complex('11.19', '21.19')
print(data)
print('Real part of data:',data.real)
print('Imaginary part of data', data.imag)```

In the above code, we have taken both parameters as a string. So we will get an error. See the below output.

```➜  pyt python3 app.py
Traceback (most recent call last):
File "app.py", line 1, in <module>
data = complex('11.19', '21.19')
TypeError: complex() can't take second arg if first is a string
➜  pyt```

## #Create complex Number Without Using complex()

It’s possible to create the complex number without using a complex() method.

For that, you have to put the ‘j’ or ‘J’ after the number.

See the following example in which we will find the datatype of the number.

```# app.py

el = 11j
print('el =', el)
print('Type of el is',type(el))```

See the following output.

```➜  pyt python3 app.py
el = 11j
Type of el is <class 'complex'>
➜  pyt```

## #Real and Imaginary Part of Complex Number

Okay, we can find the real and imaginary part of the number using the real and imag properties.

See the following code example.

```# app.py

data = complex(11, 21)
print(data)
print('Real part of data:',data.real)
print('Imaginary part of data', data.imag)```

See the output.

```➜  pyt python3 app.py
(11+21j)
Real part of data: 11.0
Imaginary part of data 21.0
➜  pyt```

## #Passing float type values

Okay, now let’s take an example in which we pass the float values as real and imag parameters.

```# app.py

data = complex(11.19, 21.19)
print(data)
print('Real part of data:',data.real)
print('Imaginary part of data', data.imag)```

See the output.

```➜  pyt python3 app.py
(11.19+21.19j)
Real part of data: 11.19
Imaginary part of data 21.19
➜  pyt```

Finally, Python complex() Example | complex() Function In Python is over.

This site uses Akismet to reduce spam. Learn how your comment data is processed.