In this tutorial, we are going to learn all about the Python complex() function. complex function in python is used to return the complex number with a real and imaginary number.
In the previous python tutorial, we have seen all about the Python chr() function along with examples.
Python complex() function
Python complex function is a Python built-in function that is used to return the complex number by specifying the real and imaginary number.
Syntax
The syntax of complex function in python is:-
complex(real, imaginary)
Parameter
complex method in python accepts two parameters:-
- real:- Required, A number that represents the real part of the complex number.
- imaginary:- Optional, A number representing the imaginary part of the complex number.
Return Value
The return value of the complex method in python is a complex number with real and imaginary part.
Python complex function example
Here we will take some examples to understand python complex method.
Example 1:
Use complex function with single parameter.
result = complex(12)
print(result)
Output will be:- (12+0j)
Example 2:
Use complex function with first and second parameter.
result = complex(23, 30)
print(result)
Output will be:- (23+30j)
Example 3:
Convert a string into complex number:-
x = '30+50j'
result = complex(x)
print(result)
Output will be:- (30+50j)
Conclusion
In this tutorial, you have learned all about the Python complex function that is a part of Python’s built-in functions.
This is the best function to create complex numbers as well as convert strings to complex numbers.
If this article helped you, please keep visit for further python built-in functions tutorials.
Other Python built-in functions
- Python abs() function
- Python all() function
- Python any() function
- Python bin() function
- Python bool() function
- Python callable() function
- Python chr() function
For more information:- Click Here