0
0
PythonProgramBeginner · 2 min read

Python Program to Convert Kilometers to Miles

You can convert kilometers to miles in Python by multiplying the kilometers value by 0.621371. For example, miles = kilometers * 0.621371.
📋

Examples

Input0
Output0.0 miles
Input5
Output3.106855 miles
Input100
Output62.1371 miles
🧠

How to Think About It

To convert kilometers to miles, you multiply the number of kilometers by the conversion factor 0.621371 because 1 kilometer equals approximately 0.621371 miles. This simple multiplication gives the equivalent distance in miles.
📐

Algorithm

1
Get the distance in kilometers from the user
2
Multiply the kilometers by 0.621371 to convert to miles
3
Display the result in miles
💻

Code

python
kilometers = float(input("Enter distance in kilometers: "))
miles = kilometers * 0.621371
print(f"{kilometers} kilometers is equal to {miles} miles")
Output
Enter distance in kilometers: 5 5.0 kilometers is equal to 3.106855 miles
🔍

Dry Run

Let's trace the input 5 kilometers through the code

1

Input kilometers

User inputs 5, so kilometers = 5.0

2

Calculate miles

miles = 5.0 * 0.621371 = 3.106855

3

Print result

Output: '5.0 kilometers is equal to 3.106855 miles'

kilometersmiles
5.03.106855
💡

Why This Works

Step 1: Get input

The program asks the user to enter the distance in kilometers using input() and converts it to a floating-point number with float().

Step 2: Convert kilometers to miles

It multiplies the kilometers by the fixed conversion factor 0.621371 to get the equivalent miles.

Step 3: Display output

The program prints the result using an f-string to show the original kilometers and the converted miles clearly.

🔄

Alternative Approaches

Using a function
python
def km_to_miles(km):
    return km * 0.621371

km = float(input("Enter kilometers: "))
miles = km_to_miles(km)
print(f"{km} kilometers = {miles} miles")
This approach organizes the conversion into a reusable function, making the code cleaner and easier to maintain.
Using a constant variable
python
CONVERSION_FACTOR = 0.621371
km = float(input("Kilometers: "))
miles = km * CONVERSION_FACTOR
print(f"{km} km is {miles} miles")
Using a named constant improves readability and makes it easy to update the conversion factor if needed.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a single multiplication and input/output operations, so it runs in constant time.

Space Complexity

It uses a fixed amount of memory for variables, so space complexity is constant.

Which Approach is Fastest?

All approaches run in constant time and space; using a function or constant variable mainly improves code clarity, not speed.

ApproachTimeSpaceBest For
Direct calculationO(1)O(1)Simple quick conversion
Function methodO(1)O(1)Reusable code in larger programs
Constant variableO(1)O(1)Readability and easy updates
💡
Always use the precise conversion factor 0.621371 for accurate kilometer to mile conversions.
⚠️
Beginners often forget to convert the input to a number, causing errors when multiplying.