In this post, we will be investigating the Python Program to Convert Kilometres to Miles. We will convert kilometres to miles and clarify the linguistic structure; simply referred to as syntax of the program.
Algorithm to Convert Kilometres to Miles
In order to convert kilometres into miles, we have have a conversion factor of 0.621371
. We will use three variables; kil, mil and con_fac to represent the distance in kilometres, distance in miles and conversion factor.
Let us take a look to the algorithm to convert kilometres to miles in Python.
Step 1: Start
Step 2: Initialize the value for kil variable
Step 3: Multiply the kil value with con_fac and get mil i.e.
mil = kil * con_fac
Step 4: Display the mil value i.e.
print(mil)
Step 5: End
New to Python Programming? Start with Python Programming!
Convert Kilometres to Miles in Python
Focusing on the program to convert kilometres to miles, let’s jump into the source code.
Example 1: Program with static value
# kilometre value kil = 20 #conversion factor value con_fac = 0.621371 # calculating miles mil = kil * con_fac print('%0.2f kilometres is equal to %0.2f miles' %(kil, mil))
The output of the above code snippet is;
20.00 kilometres is equal to 12.43 miles
Example 2: Program with dynamic input
# kilometre value kil = float(input('Enter value in kilometres: ')) #conversion factor value con_fac = 0.621371 # calculating miles mil = kil * con_fac print('%0.2f kilometres is equal to %0.2f miles' %(kil, mil))
The output of the above code snippet is;
20.00 kilometres is equal to 12.43 miles
Explanation
The basic idea behind the syntax of the function is that we have 1 kilometre is equal to 0.621371 miles, so we know we can get the equivalent miles by multiplying the kilometres value with this factor (i.e. conversion factor).
Here, the user is asked to enter the kilometres value which is then stored in the kil
variable. After that, it is multiplied with the conversion factor to finally get the equivalent miles to the kilometres value which is stored in mil
variable.
You may also check —>