It can be so frustrating that the entire planet can't just agree on one system of measurement, whether it relates to measuring distances, weights, temperatures, etc (for the record...same goes for which side of the road everyone drives on). If every country used the same units of measurement, then these formulas would be useless and obsolete, but until then, it might be a good idea to understand how to convert miles to kilometers, and vice versa. If you're a Python developer, then knowing how to write the formulas in Python might be especially useful. Keep reading to see just how it's done.

Converting miles to km isn't especially difficult. To do a rough estimate of the conversion in your head, all you really need to remember is that a mile equals about 1.6 kilometers (or that a kilometer is approximately 2/3 of a mile). When trying to find the correct conversion using a formula, we must use the more precise conversion factor, which is equal to 0.62137119.

To convert miles to kilometers, the formula is very straightforward. All you need to do is divide the number of miles by the conversion factor. To see how it would look written out in Python, check out the example below:

miles = 30
conversion_factor = 0.62137119

kilometers = miles / conversion_factor
print kilometers

In the example above, you simply need to declare two variables: miles, which can be whatever number you need, and conversion_factor, the value of which must always stay the same in order to get a correct and accurate conversion. The output of the code above would be 48.2803202, so 30 miles equals about 48 kilometers.

To convert from kilometers to miles, you'll take a similar approach. When doing this conversion, you need to multiply the kilometer value by the conversion factor. For an example of how it would look to do this conversion in Python, see the example below:

kilometers = 6
conversion_factor = 0.62137119

miles = kilometers / conversion_factor
print miles

The output of the example above would be 3.72822714, so 6 kilometers equals about 3 and three

To Practice: Try this interactive course on the basics of Lists, Functions, Packages and NumPy in Python.