Python Complex() Function With Examples

In this article, we will learn about python complex() function with examples.

Getting Started

The complex() function is another built-in function in Python that creates a complex number from a real part and an optional imaginary part.

It means that the complex() function returns a complex number from real part and imaginary part taken as parameter. When the imaginary part does not exist, the complex() function replace it by zero.

Syntax of complex() Function

The syntax of complex() function in Python is shown below –

 
complex(real, imag)

As show in above syntax, we can notice that the complex() function takes two parameters –

  1. First Argument (Real Value): The first parameter named real is considered as the real part of the complex number. Default value is 0.
  2. Second Argument (Imaginary Value): The second parameter named imag is considered as the imaginary part of the complex number. The imaginary part is optional. So, it can be equal to zero.

If the complex() function doesn’t have a parameter, it returns an imaginary number that equal to zero. For example,

 
print(complex())

Output:

0j

Here, we got 0j as output because there is no parameter for complex() function. This output is an imaginary number. An imaginary number is followed by the letter j.

Parameters of complex() Function

As explained above, complex() function takes two arguments –

  1. First argument is a real number
  2. Second argument is an imaginary number

The complex function can have no argument or one argument two arguments.

Return Value of complex() Function

In Python, complex() function returns a complex number. Thus, the complex number can contain a real part and an imaginary part.

The format of returned complex number is as shown below –

 
a + bj

Here,

  • a is real part of complex number.
  • b is imaginary part of complex number.

Examples of complex() Function with Output

Now, we will see different examples of complex() function in python –

Example 1 – Convert an Integer into Complex Number

We can convert an integer value into complex number using python complex() function as below –

  • Accepting only one Parameter

     
    a = 4
    print(complex(a))
    

    Output:

     
    (4+0j)
    

    Here,
    Default value of imaginary part is 0. Hence, output is (4+0j)

  • Accepting two Parameters

     
    a = 6
    b = 8
    print(complex(a, b))
    

    Output:

     
    (6+8j)
    

    Here,
    real part and imaginary part both have been passed to complex() function which results 6+8j.

Example 2 – Convert an Float into Complex Number

We can convert an float value into complex number using python complex() function as below –

  • Accepting only one Parameter

     
    a = 7.2
    print(complex(a))
    

    Output:

     
    (7.2+0j)
    

    Default value for imaginary part is 0 if not provided in complex() function. Hence, output is (7.2+0j).

  • Accepting two Parameters

    If we provided both real and imaginary part, output will be like below –

     
    a = 4.3
    b = 2.8
    
    print(complex(a, b))
    

    Output:

     
    (4.3+2.8j)
    

Example 3 – Converting String into Complex Number

If parameter to complex() function in python is string, then it must be valid complex number. Otherwise, we get ValueError.

  • Accepting Only One Parameter

     
    a = "4"
    print(complex(a))
    

    Output:

     
    (4+0j)
    

    The variable, a, contains only a real part in string format and we get (4+0j) as output. When the imaginary part doesn’t not exist, the complex() function replace it by zero.

  • Accepting Two Parameters

     
    number = "1+2j"
    print(complex(number))
    

    Output:

     
    (1+2j)
    

    In the above example, we are trying to convert the variable named number into a complex number. So, we get (1+2j) as output.

    Let’s take another example –

     
    number = "1 + 2j"
    print(complex(number))
    

    Output:

     
    ValueError: complex() arg is a malformed string
    

    In this case, we got a ValueError as output. That’s because there is a space before and after the operator of addition (i.e. +) in the variable number.

    So, we must keep in our mind that there should not be space before and after addition operator when we want to convert a string number into a complex number.

    In other words, string value must be a valid complex number.

Example 4 – When There is only Imaginary Part

 
number = 3j
print(complex(number))

Output:

3j

In this Python code,
We are trying to convert the variable named number into a complex number.

Example 5 – When value is already a Complex Number

 
number = 1 + 3j
print(complex(number)) 

Output:

 
(1+3j)

Here, we can notice that there is space before and after the operator of addition. But, we didn’t get a ValueError as output because the type of variable number is not a string but a complex number. That is the reason why we got this output.

That’s end of our post on Python Complex() Function With Examples.

Reference: Official Doc

Leave a Reply