In this post, we will write a Python program to convert kilometers to miles with a detailed explanation and examples. So let’s start with the very basic (formula).

Formula to Convert Kilometer to Miles

Basically, 1 kilometer is approximately equal to 0.621371 miles. Therefore, the formula to convert kilometers to miles is:

Miles = Kilometers * 0.621371

Algorithm to Convert Kilometer to Miles in Python

  1. Take user input in KM and store it in variable called km.
  2. Create a variable called miles and do miles = Kilometers * 0.621371.
  3. At the end print the value of miles.

From the above algorithm now we know how we can write a python program to convert kilometer to miles but before we jump into programming part few programming concepts you have to know:

Source Code

km = int(input("Enter value in KMs: "))

miles = km * 0.621371
print(f"{km} kms will be {miles} Miles")

Output

Enter value in KMs: 5
5 kms will be 3.106855 Miles

Additionally, you can modify the above program and rewrite it using functions. Let’s explore how we can do this.

Convert Kilometers to Miles in Python Using Function

Before writing this program you should know about:

Source Code

def km_to_miles(km):
    return km * 0.621371

km = int(input("Enter value in KMs: "))
print(f"{km} kms will be {km_to_miles(km)} Miles")

Output

Enter value in KMs: 7
7 kms will be 4.349597 Miles
Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment