This program demonstrates how you can convert kilometers to miles in Python.
The conversion of Kilometer to miles is a kind of a simple calculation. The formula to calculate the mile from kilometers is below.
1 Mile = 1.60934 Kilometers.
All that we need to do is do multiple the input (kilometers) by 0.621371 to get the total miles.
How to Convert Kilometers to Miles in Python?
# Author : Abandantcode.com # Program to convert kilometers to miles # Number of Kilometers = 10 , you can fetch this from the user too (is needed) kilometers = 10 # conversion factor KmToMileConversionFactor = 0.621371 # calculate the miles miles = kilometers * KmToMileConversionFactor print('%0.2f kilometers is equal to %0.2f miles' %(kilometers,miles))
Output
10.00 kilometers is equal to 6.21 miles
In the above program, we are converting 10 kilometers to miles. This is stored in the variable kilometers.
The input from the kilometers variable is multiplied by the conversion factor and the result is stored in the variable miles and display in the UI.
Leave a Reply