Complex() in Python | Python complex() Function
Overview
The complex in python programming is a method we can use to convert a string or a number into a complex data type. The complex() method takes two parameters, namely the real and imaginary values. The complex in python uses the provided parameters and returns a complex number.
Syntax of complex() in Python
The syntax of the complex in python is straightforward, it takes two parameters(the real value and the imaginary value)
Parameters of complex() in Python
The complex in python takes two parameters. The two parameters are: real and imaginary.
real
The first parameter, i.e., the real part, is a required parameter. The first parameter's default value, i.e., real part, is set to 0.
imaginary
The second parameter, i.e., the imaginary part, is optional. The second parameter's default value, i.e., imaginary part, is set to 0.
Note:
- The real part or the first parameter in the complex method can be a string. If the string in the parameter is a complex number (like 10 + 7j), then we should not provide the second parameter, i.e., the imaginary part.
- A complex number can be written as a real number multiplied by the imaginary unit i.
Return Values of complex() in Python
The complex in python generates a complex number using the parameters passed and returns the generated complex number.
Exceptions of complex() in Python
We can provide two parameters in the complex method in python. If the parameter provided is a string data type, it must be a valid complex number (like 2 + 3j). Otherwise, the complex method returns (or raises) an exception called ValueError. The exception raised will be: ValueError: complex() arg is a malformed string.
The ValueError is a built-in exception raised by python methods or functions when a function receives a correct argument type but with an appropriate value.
Example of complex() in Python
Let us take some examples to understand python's complex() method.
- When both the parameters are provided to the complex() method:
Output:
- When only the first parameter is provided to the complex() method:
Output:
What is Complex() function in Python?
The complex data type in python consists of two values, the first one is the real part of the complex number, and the second one is the imaginary part of the complex number. We usually denote the real part using i and the imaginary part with j. For example, (3 + 7j) or (3i + 7j).
To convert a string or a number into a complex number data type in python, we can use a method called complex() in python.
The complex() method takes two parameters as input: the real part of the complex number and the imaginary part of the complex number. The first parameter is necessary. On the other hand, the second parameter is optional. The complex() method returns the complex data type in python generated using the parameters provided.
We can provide a string, a number, or even a complex number as a parameter to the complex() method. If a string is provided in the parameter, it should be a valid complex number (like 10 + 7j). Otherwise, the complex method returns (or raises) a built-in exception called ValueError.
Note:
The complex() method is not the only way of creating complex data types in python. Check the More Examples section below for an explanation.
More Examples
So far, we have learned about the complex() method of python. Now let us take some examples and understand the method better.
Convert a String into a Complex Number
When the provided parameter is a string data type, it must be a valid complex number (like 2 + 3j). Otherwise, the complex method returns (or raises) an exception called ValueError.
- If the string provided in the parameter is a valid complex number.
Output:
- If the string provided in the parameter is an invalid complex number.
Output:
Parameter Having an Integer Value
We can also provide the real number part of the complex number as a string. For example, in the complex number 3 + 0j, we can pass the real number 3 as a string parameter (like '3').
Output:
Working With Float Type Values
We can also provide the real number part of the complex number as a floating-point number. For example, in the complex number 5.3 + 0j, we can pass the real number 5.3 as a string parameter (like '5.3').
Output:
Working With Complex Numbers as a Parameter
We can even provide a complex number as a parameter in the complex() method. For example, we can pass 2+3j as a parameter.
Output:
Creating Complex Number Without Using complex()
The complex in python is not the only way to create complex numbers. We can put I and j after the real and imaginary parts, respectively to generate the real number.
Output:
Conclusion
- The complex in python is a method we can use to convert a string or a number into a complex number.
- The syntax of the complex in python is: complex(real, imaginary).
- The complex in python, takes two parameters, the first parameter is required , but the second is optional.
- The complex in python generates a complex number using the parameters passed and returns the generated complex number.
- If a string is provided in the parameter, it should be a valid complex number; otherwise, the ValueError exception is raised.