Python program to convert kilometers to miles

Published by user on

In this tutorial, we are going to look at how to convert kilometers to miles using python.

Both kilometers and miles are units to measure distance.

Let’s look at the relation between them.

1 Kilometer = 0.621371 Miles

1 Mile = 1 / 0.621371 Kilometers

Now we know the conversion factor. So, let’s look at the example.

Program: Convert Kilometer to Miles

# Accept the user input in kilometers
kilometers = float(input("Enter the distance in kilometers: "))

# conversion factor to convert kilometers to miles
conversion_factor = 0.621371

# here the actual conversion happens
miles = kilometers * conversion_factor
print('%0.2f kilometers is equivalent to %0.2f miles.' %(kilometers,miles))

Output

Enter the distance in kilometers: 5
5.00 kilometers is equivalent to 3.11 miles.

Steps

  1. Accept the user input in the kilometers variable
  2. Calculate miles using the formula, miles = kilometers * 0.621371
  3. Print miles
Categories: python