Python Program to Convert Kilometers to Miles

In this example you will learn how to convert kilometers to miles using a Python program.

# Get the distance in kilometers from the user
kilometers = float(input("Enter distance in kilometers: "))

# Convert kilometers to miles
miles = kilometers / 1.609

# Output the result
print(f"{kilometers} kilometers is equal to {miles:.2f} miles.")

In this program, we start by asking the user to enter a distance in kilometers using the input() function. We store the distance as a floating-point number in a variable called kilometers.

We then use the conversion factor of 1.609 kilometers per mile to convert the distance to miles and store the result in a variable called miles.

Finally, we output the converted distance using an f-string, with two decimal places of precision.

Table of Contents

Output:

Enter distance in kilometers: 42.195
42.195 kilometers is equal to 26.22 miles.

Enter distance in kilometers: 10
10.0 kilometers is equal to 6.21 miles.

Enter distance in kilometers: 10000
10000.0 kilometers is equal to 6213.71 miles.

Leave a Comment