DISCOVER
×

How to Convert Polar to Cartesian in Python

Updated February 21, 2017

One of the benefits of using the Python programming language is that it is easy to learn, understand and implement. It's portable in that it works equally well on all computer platforms, it's easy to use and it doesn't have the same syntax rules that lower-level languages such as C and C++ have. Use Python to help you solve everyday math problems, such as converting a set of polar coordinates to Cartesian coordinates.

Import the math module into your Python script. At the beginning of the script, add the following:

import math

or

from math import *

The difference between the two is the way functions will be called in your program later. In the first example, to print the sin of "x," type:

print math.sin(x)

In the second example, type:

print sin(x)

Obtain the polar coordinates from the user:

r = raw_input("Please enter the r coordinate: ")

theta = raw_input("Please enter the theta coordinate: ")

Use the conversion formulas to convert "r" and "theta" to "x" and "y":

x = r * cos(theta)

y = r * sin(theta)

Output the results:

print "The Cartesian coordinates for (",r,",",theta,") are (",x,",",y,")"

Another way of formatting it is as follows:

print "The Cartesian coordinates for (%s,%s) are (%s,%s)" % (r, theta, x, y)

Tip

Make sure you import the math module. Without it, the Python interpreter will return an error at the cos and sin functions.

bibliography-icon icon for annotation tool Cite this Article

About the Author

Wolfram Donat is an information technology professional and writer currently finishing a degree in computer systems engineering. He has had success writing for both on- and offline audiences, and moonlights as a professional copywriter as studies permit. He is currently shopping his first novel to agents and publishers, and is hard at work on his second.