complex(): Get Complex Number Function

complex

LanguagePython
CategoryFunction
Part OfBuilt In Functions
Named Arguments Count2
Unnamed Arguments Count1/multiple
Official Documentation complex Function

1 Description

The Python complex function is used to obtain a complex number. The complex number is based on the supplied arguments. The first option is to pass a number to the complex function. The second option is to pass a string to the complex function. The third option is to pass a real and imaginary part to the complex function.

2 Prototypes

2.1 String

Below is the string prototype, for when you want to pass a single string to the complex function.

complex(string)

2.2 Single Numeric

Below is the 'single numeric' prototype for when you want to pass a number to the complex function.

complex(number=0)

2.3 Double Numeric

Below is the 'double numeric' prototype. For this prototype the first argument will be interpreted as the 'real' component, and the second argument will be interpreted as the 'imaginary' component.

complex(real=0, imag=0)

3 Arguments

complex Function Arguments
Name Type Default Value Category Description
number numeric 0 Keyword argument The numeric value you would like converted into a complex number.
string string Unnamed argument The string which contains a number that you would like converted into a complex number.
real numeric 0 Keyword argument The real component of a real/imaginary pair that you would like converted into a complex number.
imag numeric 0 Keyword argument The imaginary component of a pair that you would like converted into a complex number.

4 Returns

complex Function Returns
Return Type Explanation
complex The complex function will always return an object of the 'complex' type.

5 Examples

5.1 Create Complex Number

In the python file below we make a single call to the complex function to create a complex number out of the numeric value 5.

#!/usr/bin/python3

def main():
    var_1 = complex(5)  # creating a complex from an int
    var_2 = complex(5.93)  # creating a complex from a float

    var_3 = complex("49")  # creating a complex from a string
    var_4 = complex("49.394")  # creating a complex from a string

    var_5 = complex(5, 10)  # creating a complex from two real and imaginary components
    var_6 = complex(real=5, imag=10)  # creating a complex from two real and imaginary components

    var_7 = complex()  # creating a complex from an empty constructor (default argument values will be used)

    print("var_1:", var_1)
    print("var_2:", var_2)
    print("var_3:", var_3)
    print("var_4:", var_4)
    print("var_5:", var_5)
    print("var_6:", var_6)
    print("var_7:", var_7)

if __name__ == "__main__":
    main()

Now we can run the python file and observe the complex number object being printed to STDOUT.

user-1@vm:~/Documents$  ./complex_example_1.py
var_1: (5+0j)
var_2: (5.93+0j)
var_3: (49+0j)
var_4: (49.394+0j)
var_5: (5+10j)
var_6: (5+10j)
var_7: 0j
user-1@vm:~/Documents$ 

This document was last updated: