Python complex() class
The complex() class in Python returns a complex number or converts a string or number to a complex number.
Syntax
1 2 3 |
class complex([real[, imag]]) |
Parameter
Real: This parameter consists of a number representing the real part of the complex number. By default, its value is 0. The real number can also be a String, like this ‘3+5j’, when this is the case, the second parameter should be omitted.
Imaginary: It is an optional parameter which consists of a number representing the imaginary part of the complex number
Return
It returns a complex number by specifying a real number and an imaginary number.
Example 1
1 2 3 4 5 6 7 8 9 |
# Python Program explaining # complex() class val1 = complex(11) # Passing single parameter val2 = complex(11,12) # Passing two parameters # passing the output print(val1) print(val2) |
Output
1 2 3 4 |
(11+0j) (11+12j) |
Example 2
1 2 3 4 5 6 7 8 9 |
# Python Program explaining # complex() class val1 = complex(1.5) # Passing single integer parameter val2 = complex(1.5,2.2) # Passing double integer parameters # passing the output print(val1) print(val2) |
Output
1 2 3 4 |
(1.5+0j) (1.5+2.2j) |
Example 3
1 2 3 4 5 6 7 8 |
# Python Program explaining # complex() class val1 = complex(1+2j) val2 = complex(1+2j,2+3j) print(val1) print(val2) |
Output
1 2 3 4 |
(1+2j) (-2+4j) |