Change language

Complex Numbers in Python | Kit 1 (Introduction)

|

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.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically