This article covers the use of the Python complex Function.
This article covers the use of the python complex function and it’s uses complete with examples.
A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers, and i is a solution of the equation x² = −1. Because no real number satisfies this equation, i is called an imaginary number.
The complex function takes integers or strings as inputs and returns an appropriate complex number. Attempting to pass a inappropriate value will possibly result in a ValueError
.
Syntax
complex(real, imag)
The complex()
takes two parameters, real
and imag
.
real
, The real part of the number. If left empty, will take the default value of0
.imag
, The imaginary part of the number. If left empty will take the default of0
.
You can also choose to pass a single string instead of two numbers. See the examples below for clarification.
Example 1
>>> complex(3)
(3+0j)
>>> complex(0,7)
7j
>>> complex(3,4)
(3+4j)
>>> complex(-3,-2)
(-3-2j)
Example 2
>>> complex('3+4j')
(3+4j)
>>> complex('5-2j')
(5-2j)
Conjugates
The conjugate of a complex number can be thought of it’s opposite. The conjugate of a complex number is found by multiplying the imaginary part by the -
sign. Any complex number returned from the complex()
function can have the conjugate()
function applied to it.
>>> a = complex(3+4j)
>>> a.conjugate()
(3-4j)
>>> b = complex(5-1j)
>>> b.conjugate()
(5+1j)
This marks the end of the Python complex Function. Any suggestions or contributions for CodersLegacy are more than welcome. Questions can be asked in the comment section below.
Here’s a link back to the main Python Built-in Functions page.