Python Program to Convert Kilometers to Miles

159
views
Kilometers to Miles

In this program, you’ll learn how to Convert Kilometers to Miles in Python.

To properly understand this Program to Convert Kilometers to Miles, you should have the knowledge of following Python programming topics:

  1. Python Input, Output and Import
  2. Python Variables and Data Types
  3. Python Operators

Converting the miles to km isn’t 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 as we used in Mathematics. 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

Program to Convert Kilometers to Miles.

kilometers = 5.5

# To take kilometers from the user, uncomment the code below
kilometers = input("Enter value in kilometers")

# conversion factor
conv_fac = 0.621371

# calculate miles
miles = kilometers * conv_fac
print('%0.3f kilometers is equal to %0.3f miles' %(kilometers,miles))

Output:

5.500 kilometers is equal to 3.418 miles

Note: To test the program, change the value of kilometres.
By using this formula kilometers = miles / conv_fac you can convert miles to kilometers.

Ask your questions and clarify your/others doubts on Program of Conversion of Kilometers to Miles by commenting. Documentation