Complex Numbers in Python | Kit 1 (Introduction)

File handling | Python Methods and Functions

Converting real numbers to complex numbers

A complex number is represented as " x + yi ". Python converts real numbers x and y to a complex using the complex (x, y) function. The real part can be accessed using the real () function, and the imaginary part can be represented by imag () .

 ` # Python code to demonstrate how it works ` ` # complex (), real () and imag () `   ` # import & quot; cmath & quot; for operations with complex numbers ` ` import ` ` cmath `   ` # Initializing real numbers ` ` x ` ` = ` ` 5 ` ` y ` ` = ` ` 3 `   ` # convert x and y to complex number ` ` z ` ` = ` ` complex ` ` (x, y); `   ` # printing real and imaginary parts of a complex number ` ` print ` ` (` ` "The real part of complex number is:" ` `, end ` ` = ` ` "") ` ` print ` ` (z.real) `   ` print ` ` (` ` "The imaginary part of complex number is: "` `, end ` ` = ` `" ") ` ` print ` ` (z.imag) `

Output:

` The real part of complex number is: 5.0 The imaginary part of complex number is: 3.0 `

Complex phase th number

Geometrically, the phase of a complex number — it is the angle between the positive real axis and the vector representing the complex number . This is also known as the argument of a complex number. Phase is returned by phase () , which takes a complex number as an argument. The phase range is from -pi to + pi. that is, from -3.14 to +3.14 .

 ` # Python code to demonstrate how it works ` ` # phase () `   ` # import & quot; cmath & quot; for operations with complex numbers ` ` import ` ` cmath `   ` # Initializing real numbers ` ` x ` ` = ` ` - ` ` 1.0 ` ` y ` ` = ` ` 0.0 `   ` # convert x and y to complex number ` ` z ` ` = ` ` complex ` ` (x, y); `   ` # printing the phase of a complex number using phase () ` ` print ` ` (` ` "The phase of complex number is:" ` `, end ` ` = ` ` "") ` ` print ` ` (cmath.phase (z)) `

Output:

` The phase of complex number is: 3.141592653589793 `

Conversion from polar to rectangular and vice versa

Conversion to polar is performed using polar () , which returns a pair (r, ph), denoting module r and phase angle ph . a module can be rendered using abs (), and a phase using phase ()
A complex number is converted to rectangular coordinates using rect (r, ph) , where r — module, and ph — phase angle . Returns a value numerically r * (math.cos (ph) + math.sin (ph) * 1j)

 ` # Python code to demonstrate how it works ` ` # polar () and rect () `   ` # import & quot; cmath & quot; for operations with complex numbers ` ` import ` ` cmath ` ` import ` ` math `   ` # Initializing real numbers ` ` x ` ` = ` ` 1.0 ` ` y ` ` = ` ` 1.0 ` ` `  ` # convert x and y to complex number ` ` z ` ` = ` ` complex ` ` (x, y); `   ` # convert complex number to polar using polar () ` ` w ` ` = ` ` cmath.polar (z) `   ` # print engine and polar complex number argument ` ` print ` ` (` ` "The modulus and argument of polar complex number is:" ` `, end ` ` = ` ` "") ` ` print ` ` (w) `   ` # convert complex number to rectangular using rect () ` ` w ` ` = ` ` cmath.rect (` 1.4142135623730951 `, ` ` 0.7853981633974483 ` `) `   ` # printing a rectangular complex number ` ` print ` ` (` ` "The rectangular form of complex number is:" ` `, end ` ` = ` ` "") ` ` print ` ` (w) `

Output:

` The modulus and argument of polar complex number is: (1.4142135623730951, 0.7853981633974483) The rectangular form of complex number is: (1.0000000000000002 + 1j) `

Manjit Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.