  # How do I use the complex () method in Python?

Python Methods and Functions

The complex () method in Python is used to create a complex number from other primitive data types. This is useful when you want to quickly perform complex arithmetic operations and conversions. ## Syntax

This function returns a complex number which contains real and imaginary parts. A complex number refers to the data type ` class complex `.

Therefore, function call syntax:

` class complex ([real [, imag]]) `

The complex number will be:

` com plex_number = real + imag * j `

Here ` j ` &imaginary number (sqrt (-1))

Python's ` complex () ` method will return this complex number.

You might think that you can use use only integers and floating point numbers, but this is not the case.

We can also construct it from a string, hexadecimal, binary, or even another complex number.

## Calling complex ( ) without parameters

We can call this method without passing any arguments to it. This will return a zero complex number ` 0j `.

` a = complex () print (type (a)) print (a ) `

Exit

` & lt; class 'complex' & gt; 0j `

## Calling complex () using numeric parameters

This will create the desired complex number in the form ` a + bj `, where ` a ` and ` b ` &numeric parameters.

` a = complex (1, 2) print (a) b = complex (1, 1.5) print (b) c = complex (-1.5, 3.414) print (c) `

Exit

` (1 + 2j) (1 + 1.5j) (-1.5 + 3.414j) `

## Call with hex and binary arguments

We can also directly pass to this hexadecimal or binary number without converting it to an integer.

` a = complex (0xFF) # hexadecimal print (a) b = complex ( 0b1010, -1) # binary print (b) `

Exit

` (255 + 0j) (10-1j) `

## Call using another complex number

We can also pass another complex number when constructing a complex number using `complex()`

` a = 1 + 2j b = complex (a, -4) # Cons truct using another complex number print (b) c = complex (1 + 2j, 1 + 2J) print (c) `

Exit

` (1-2j) (-1 + 3j) `

Since both arguments are added in the form ` a + b * j `, in the first case the result will be: 1 + 2j + (-4 * j) = 1-2j

In the second case, we have (since j * j = -1): 1 + 2j + (1 + 2j) * j = (1 + 2j + j &2) = -1 + 3j

## Using string arguments

We can also pass a string with it, provided there are no spaces between it. The string should only be of the form "a + bj", that is, a string that can represent a complex number.

If we use a string, we can only use one argument.

` a = complex ("1 + 2j") print (a) `

Exit

` (1 + 2j) `

If the string contains spaces or any other extraneous character, a ` ValueError ` exception is thrown.

` b = complex (" 2 + 4j ") print (b) `

This will throw a ` ValueError ` because there are spaces in the string.

Likewise, if we pass two arguments to ` complex () `, it will throw a ` TypeError `, as we are not allowed to pass multiple arguments if the first argument is a String.