# Python complex: The Complete Guide

The “x + yi represents the complex number”. Python converts the real numbers x and y into the complex using the function complex(x,y). The real part can be accessed using the 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

Python complex() is a built-in function that returns a complex number by specifying a real and imaginary number. It returns a complex number when real and imaginary parts are provided or converts a string to a complex number. The complex() function converts a string or a number into a complex number.

### Syntax

See the following syntax.

`complex(real, imag)`

### Arguments

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 the 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 passing the second argument. It generates an error if we give 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()

To create a complex number without a complex() function, use 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

For example, 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```

That’s it for this tutorial.

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