Python Program to Convert Miles to Kilometers and vice versa
In this example, we will write a simple program to convert units of measurement from miles to kilometers and vice versa in Python. The conversion formula plays an important role in writing the program. Let us see how the formula looks:-
To Convert Miles to Kilometers
1 Mile = 1.60934 Kilometers
1 Kilometer = 0.621371 Miles
1 Mile = 1.60934 Kilometers
1 Kilometer = 0.621371 Miles
To better understand this example, make sure you have knowledge of the following tutorials:-
Contents
Python Program to Convert Miles to Kilometers:
miles = float(input("Enter the value in miles: ")) conversion_factor = 1.60934 kilometers = miles * conversion_factor print('%.4f miles = %0.4f kilometers' %(miles, kilometers))
The output of the above program is:-
Enter the value in miles: 4
4.0000 miles = 6.4374 kilometers
4.0000 miles = 6.4374 kilometers
Python Program to Convert Miles to Kilometers:
kilometers = float(input("Enter the value in kilometers: ")) conversion_factor = 0.621371 miles = kilometers * conversion_factor print('%0.4f kilometers = %0.4f miles' %(kilometers, miles))
The output of the above program is:-
Enter the value in kilometers: 2
2.0000 kilometers = 1.2427 miles
2.0000 kilometers = 1.2427 miles