Python Program - Convert Kilometers to Miles
Python Program - Convert Kilometers to Miles
To convert Kilometers to Miles in Python, you can use the following formula in the program.
Miles = Kilometers * 0.621371
Let us write a function, that takes the Kilometers value as argument, computes the Miles using the above formula, and returns the result.
def kilometersToMiles(km):
return km * 0.621371
You may use this function, in your program, to convert Kilometers to Miles.
The following is an example program, where we shall use the above conversion function, and convert 10
Kilometers to Miles and print the result to output.
Python Program
# Function that converts Kilometers to Miles
def kilometersToMiles(km):
return km * 0.621371
if __name__ == "__main__":
# Given Kilometers
km = 10
# Convert Kilometers to Miles
miles = kilometersToMiles(km)
print(f"{km} Kilometers is equal to {miles} Miles")
Output
10 Kilometers is equal to 6.21371 Miles
To implement the formula, we used Python Multiplication from Arithmetic Operators.
To verify your output, you can use this online tool: ConvertĀ Kilometers to Miles online by ConvertOnline.org.