Python cmath.polar() Method with Examples

cmath.polar() Method in Python:

A complex number is converted to polar coordinates using the cmath.polar() method. It returns a tuple consisting of modulus and phase.

A complex number in polar coordinates is defined by modulus r and phase angle phi.

Syntax:

cmath.polar(x)

Parameters

x: This is Required. A number used to calculate the polar coordinates of

Return Value:

Returns a tuple value containing polar coordinates.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The polar coordinates of given complex number (3+4j)  = 
(5.0, 0.9272952180016122)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The polar coordinates of given complex number (5+2j) = 
(5.385164807134504, 0.3805063771123649)

Note: The above input format is for dynamic input.

cmath.polar() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.polar() method that converts the given complex number to polar coordinates and returns a tuple consisting of modulus and phase.
  • Store it in another variable.
  • Print the result tuple consisting of modulus and phase.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3+4j
# Pass the given complex number as an argument to the cmath.polar() method
# that converts the given complex number to polar coordinates and returns a
# tuple consisting of modulus and phase.
# Store it in another variable.
rslt = cmath.polar(complexnumb)
# Print the result tuple consisting of modulus and phase.
print("The polar coordinates of given complex number", complexnumb,
      " = ")
print(rslt)

Output:

The polar coordinates of given complex number (3+4j)  = 
(5.0, 0.9272952180016122)

Similarly, try for the other examples

import cmath
complexnumb = -1-2j
rslt = cmath.polar(complexnumb)
print("The polar coordinates of given complex number", complexnumb,
      " = ")
print(rslt)

Output:

The polar coordinates of given complex number (-1-2j)  = 
(2.23606797749979, -2.0344439357957027)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Pass the given complex number as an argument to the cmath.polar() method that converts the given complex number to polar coordinates and returns a tuple consisting of modulus and phase.
  • Store it in another variable.
  • Print the result tuple consisting of modulus and phase.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.polar() method
# that converts the given complex number to polar coordinates and returns a
# tuple consisting of modulus and phase.
# Store it in another variable.
rslt = cmath.polar(complexnumb)
# Print the above result tuple consisting of modulus and phase.
print("The polar coordinates of given complex number", complexnumb,
      " = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The polar coordinates of given complex number (5+2j) = 
(5.385164807134504, 0.3805063771123649)