Python Program to Convert Kilometers to Miles and Vice-versa

Python program to convert kilometers to miles and convert miles to kilometers | Kilometers is a unit of length in the metric system, equal to 1,000 meters and the equivalent of 0.6214 miles. Miles is also the unit of length equal to 1,760 yards and standardized as exactly 1,609.344 meters by international agreement in 1959. In this post, we will develop a program to convert kilometers to miles and also convert miles to kilometers.

  • 1 kilometer is equal to 0.621371 miles.
  • 1 mile is equal to 1.60934 kilometers.

Formula:-

Miles = Kilometers * 0.621371
Kilometers = Miles * 1.60934

Example:-

Distance in kilometers: 4.5
Miles = 4.5 * 0.621371 = 2.7961695
4.5 kilometers is equal to 2.80 miles

Convert Kilometer to Mile in Python

This Python program using only a mathematics formula to convert kilometers to mile. We will take the distance in Kilometers while declaring the variables and then, calculate the distance in Miles using the formula. Finally, the result will be displayed on the screen.

# Python program to convert kilometers to miles

# take inputs
km = float(input('Enter distance in kilometers: '))

# conversion factor
conv_fac = 0.621371

# calculate Miles
mile = km * conv_fac

# display result
print('%0.2f kilometers is equal to %0.2f miles' %(km, mile))

Output for the different input values:-

Enter distance in kilometers: 5
5.00 kilometers is equal to 3.11 miles

Enter distance in kilometers: 8.3
8.30 kilometers is equal to 5.16 miles

Enter distance in kilometers: 308.264
308.26 kilometers is equal to 191.55 miles

Python Program to Convert Miles to Kilometers

In the previous program, we had converted kilometers to miles but in this program, we will convert miles to kilometers in Python. We will take the distance in Miles while declaring the variables and then, calculate the distance in Kilometers using the formula. Finally, the result will be displayed on the screen.

# Python program to convert miles to kilometers

# take inputs
mile = float(input('Enter distance in miles: '))

# conversion factor
conv_fac = 1.60934

# calculate kilometers
km = mile * conv_fac

# display result
print('%0.2f miles is equal to %0.2f kilometers' %(mile, km))

Output for the different input values:-

Enter distance in miles: 2.17
2.17 miles is equal to 3.49 kilometers

Enter distance in miles: 5
5.00 miles is equal to 8.05 kilometers

Enter distance in miles: 50.3
50.30 miles is equal to 80.95 kilometers

Also See:- Celsius to Fahrenheit in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply