Python Program - Convert Miles to Kilometers


Python Program - Convert Miles to Kilometers

To convert Miles to Kilometers in Python, you can use the following formula in the program.

Kilometers = Miles * 1.60934

Let us write a function, that takes the Miles value as argument, computes the Kilometers using the above formula, and returns the result.

def milesToKilometers(miles):
    return miles * 1.60934

You may use this function, in your program, to convert Miles to Kilometers.

The following is an example program, where we shall use the above conversion function, and convert 10 Miles to Kilometers and print the result to output.

Python Program

# Function that converts Miles to Kilometers
def milesToKilometers(miles):
    return miles * 1.60934

if __name__ == "__main__":
    # Given Miles
    miles = 10

    # Convert Miles to Kilometers
    km = milesToKilometers(miles)

    print(f"{miles} Miles is equal to {km} Kilometers")

Output

10 Miles is equal to 16.0934 Kilometers

To implement the formula, we used Python Multiplication from Arithmetic Operators.

To verify your output, you can use this online tool: Convert Miles to Kilometers online by ConvertOnline.org.