The Python complex() function is used to create a complex number by combining a real part and an imaginary part.
The real part of a complex number represents the component that lies on the standard number line (the horizontal axis). It is a regular real number and can be positive, negative, or zero. In mathematical notation, if "z" is a complex number, the real part is denoted as "Re(z)".
The imaginary part of a complex number represents the component that lies on the imaginary axis (the vertical axis). It is a multiple of the imaginary unit "i" (or j in Python), where "i" is defined as the square root of "-1". In mathematical notation, if "z" is a complex number, the imaginary part is denoted as "Im(z)".
SyntaxFollowing is the syntax of Python complex() function −
complex(real [,imag])Parameters
This function takes two optional parameters as shown below −
real − It represents the real part of the complex number. If not provided, it defaults to 0.
imag (optional) − It represents the imaginary part of the complex number. If not provided, it defaults to 0.
This function returns a complex number based on the provided real and imaginary parts or a string representing a complex number.
Example 1In the following example, we are using the complex() function to create a complex number with a real part of "2" and an imaginary part of "3" −
real = 2 imaginary = 3 result = complex(real, imaginary) print('The complex value obtained is:',result)Output
Following is the output of the above code −
The complex value obtained is: (2+3j)Example 2
If we do not pass an imaginary part to the complex() function, its default value is set to 0.
Here, we are using the complex() function with only the real part "4" −
real = 4 result = complex(real) print('The complex value obtained is:',result)Output
Output of the above code is as follows −
The complex value obtained is: (4+0j)Example 3
If we do not pass a real part to the complex() function, its default value is set to 0. Here, we are using the complex() function with only the imaginary part "7" −
imaginary = 7 result = complex(imag=imaginary) print('The complex value obtained is:',result)Output
The result obtained is as shown below −
The complex value obtained is: (7+0j)Example 4
In here, we are using the complex() function without providing any real or imaginary parts. As a result, it defaults to 0j, representing a complex number with both real and imaginary parts equal to 0 −
result = complex() print('The complex value obtained is:',result)Output
Following is the output of the above code −
The complex value obtained is: 0jExample 5
In the example below, the complex() function parses the string "2+4j" and creates the corresponding complex number (2+4j) −
result = complex("2+4j") print('The complex value obtained is:',result)Output
The result produced is as follows −
The complex value obtained is: (2+4j)
python_type_casting.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4