Write a Python Program to Convert Kilometers to Miles or km to miles. Both are the international standard system of units for measuring distance. Countries like the United States and the United Kingdom measure distance using the miles metric system. The standard formula to convert is Miles = Kilometers * 0.621371.
Python Program to Convert Kilometers to Miles
This example enables users to enter the kilometers and convert them to miles. As we know, the conversion factor, one kilometer, approximately equals 0.621371 miles.
kilometers = float(input("Enter the Kilometers = ")) miles = kilometers * 0.621371 print("%.2f Kilometers equals %.4f Miles" %(kilometers, miles))
Convert M to KM Using Function
This mile to KM program is the same as the above example. However, we separated the logic using the functions.
def kmToM(km): m = km * 0.621371 return m km = float(input("Enter the Kilometers = ")) m = kmToM(km) print("%.2f Kilometers equals %.4f Miles" % (km, m))
Enter the Kilometers = 90
90.00 Kilometers equals 55.9234 Miles