This article mainly introduces the built-in function complex in the Python standard library. This article first explains the role and usage of complex, and then provides examples, you can refer to this function to create a plural number using the real + imag * j parameter. You can also convert the number of a string to the plural value, or convert a number to the plural value. If the first parameter is a string and the second parameter is not required, the string is interpreted and the plural value is returned. However, the second parameter cannot be entered as a string; otherwise, an error occurs. Real and imag parameters can enter numbers. if the imag parameter is not input, it is a zero value by default. This function is equivalent to the int () or float () function. If the real and imag parameters both input zero, this function returns 0j. With this function, you can easily convert a list into a complex number.
Note: When you want to convert a string from the plural form to the plural form, note that no space exists in the string, for example, complex ('1 + 2j '), instead of writing complex (1 + 2j '), otherwise, a ValueError exception is returned.
Example:
The code is as follows:
# Complex ()
Print (complex (1 ))
Print (complex ('2 + 1j '))
Print (complex (2, 5 ))
L = [1, 3, 4, 5]
For I in l:
Print (complex (I, 5 ))
The output is as follows:
The code is as follows:
(1 + 0j)
(2 + 1j)
(2 + 5j)
(1 + 5j)
(3 + 5j)
(4 + 5j)
(5 + 5j)