Q:

Python program to convert Kilometres to Miles

belongs to collection: Python Basic Programs

0

In this tutorial, we will learn a step-by-step approach for how to write a Python program to convert kilometres value into miles value.

Understanding the Logic Behind the Conversion of Kilometre to Miles

Let's begin with the basics; first, we will discuss the measuring units. As we know, Kilometre and Miles represent the unit of length.

1 kilometre equals 0.62137 miles.  

Miles = kilometre * 0.62137  

And,  

Kilometre = Miles / 0.62137  

 

Hence, the value "0.62137" would be considered as the factor of conversation or the ratio for converting units.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Steps to be Followed for Converting Kilometre unit to Miles Unit.

We follow the below steps for getting a clear idea of how to convert kilometres to miles units.

Step 1: We will define the variable for storing the value of kilometre_1 and accepting the input from the user.

kilometre_1 = float (input ("Please enter the speed of car in Kilometre as a unit: "))  

Step 2: Now, we will define and store the conversion factor into the variable.

conversion_ratio_1 = 0.621371  

Step 3: Then, we will define the variable for storing the value of kilometre_1 which is converted into miles. Then in addition we will write the logic of converting kilometre to miles units.

miles_1 = kilometre_1 * conversion_ratio_1  

Step 4: At last, we will display the converted value by using the print() function.

print ("The speed value of car in Miles: ", miles_1)  

Full Extension of Code:

kilometre_1 = float (input ("Please enter the speed of car in Kilometre as a unit: "))  
conversion_ratio_1 = 0.621371  
miles_1 = kilometre_1 * conversion_ratio_1  
print ("The speed value of car in Miles: ", miles_1)  

Output:

Please enter the speed of car in Kilometre as a unit:  16
The speed value of car in Miles:  9.941936

Approach 2:

We can also use another approach of simply statistically defining a function for converting kilometres to miles unit.

Example:

def kilometre_1(km):  
    conversion_ratio_1= 0.621371  
    miles_1 = km * conversion_ratio_1  
    print ("The speed value of car in Miles: ", miles_1)  
km = float (input ("Please enter the speed of car in Kilometre as a unit: "))  
kilometre_1(km)  

Output:

Please enter the speed of car in Kilometre as a unit:  14
The speed value of car in Miles:  8.699194

Conclusion

In this tutorial, we have discussed step-by-step of how to write a python code for converting kilometre unit to miles unit.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python program to convert Celsius to Fahrenheit... >>
<< Python Program to Generate a Random Number...