Convert Kilometers to Miles in Python

In this Python lesson, we will practice how to write a program to convert kilometers to miles using the formula below.

1km = 0621371

miles = kilometre(s) * 0.621371

Example: Kilometers to Miles

# Request kilometers input from the user
km = float(input("Enter km value: "))

# conversion rate
rate = 0.621371

# calculate miles
miles = km * rate
print('%0.2f km is equal to %0.2f miles' %(km,miles))

Output

Enter km value: 56

56.00 km is equal to 34.80 miles

In this example, we used the km variable which we created in the program to save the kilometer value that the user inputs and this is multiplied by the rate variable to convert kilometers to miles.

Do you want to become an expert programmer? See books helping our students, interns, and beginners in software companies today (Click here)!

Leave a Comment

Python Tutorials