Write a Python program to convert Polar coordinates to rectangular coordinates


This program demonstrates how to convert between rectangular and polar coordinates of complex numbers using the cmath module in Python.

  • In the first line, a complex number cn is created with real part 3 and imaginary part 4 using the complex() function.
  • In the second line, the cmath.polar() function is used to convert the complex number cn from rectangular coordinates to polar coordinates. The cmath.polar() function returns a tuple of two values - the magnitude (length) of the complex number and the phase (angle) of the complex number. The result is printed using the print() function.
  • In the third line, the cmath.rect() function is used to convert a complex number from polar coordinates to rectangular coordinates. The first argument of cmath.rect() is the magnitude (length) of the complex number, and the second argument is the phase (angle) of the complex number in radians. In this example, 2 is the magnitude, and cmath.pi (which is equivalent to 3.14159...) is the angle. The result is stored in cn1.
  • The fourth line prints the result of the conversion from polar coordinates to rectangular coordinates, which is cn1.

Overall, this program provides a simple implementation of how to convert between rectangular and polar coordinates of complex numbers using the cmath module in Python. It can be used to quickly convert between different representations of complex numbers in Python.

Source Code

import cmath
cn = complex(3,4)
print("Polar Coordinates: ",cmath.polar(cn))
cn1 = cmath.rect(2, cmath.pi)
print("Convert Polar to rectangular Coordinates : ",cn1)

Output

Polar Coordinates:  (5.0, 0.9272952180016122)
Convert Polar to rectangular Coordinates :  (-2+2.4492935982947064e-16j)

Example Programs