In this article, we will understand how to convert kilometers(km) to miles in python. After reading this article, you will be able to convert n km to miles in python, such as 10 km to miles.
1 km = 0.621371 miles
Python program to convert km to miles is given below
# read value to be converted in km km = float(input('Enter distance in km: ')) # convert to miles miles = km * 0.621371 print('Distance in miles is %0.4f' %miles)
Explanation
The program reads the number of kilometers as user input. User input in python is read using input()
function.
It reads the values as a string which should be converted using float()
function.
Since 1 km = 0.621371 miles, in order to convert to miles, we need to multiply the value in km by this value. Example output of this program is given below.
Enter distance in km: 3
Distance in miles is 1.8641
Note that the value in miles is displayed till 4 decimal places.