Python Program to Convert Kilometers to Miles in Python

In this article, we will see Python Program to convert Kilometer to miles and miles to kilometer (km). Miles and kilometers are two different units for the measurement of distance. For the imperial system, we use miles and for the metric system, we use KMS. Kilometers are used to measure long distances whereas miles are used to compare relatively shorter distances.

The basic formula for the conversion of Kilometer to Miles

Distance in kms = 1.609 x distance in miles.

If the user wants to perform the other way of this, which is the conversion of miles to kilometers, we use the formula below:

Distance in miles = 0.621 x distance in kms.

Python Program for conversion of Kilometers (KMS) to Miles

print("Enter kilometers")
kms = int(input())
miles= 0.621 * kms
print(miles,"miles")

As we know, Python always takes input in the string data type. Therefore, in the second line of our program, we convert our input from a string to an integer data type. This type of conversion is called explicit type conversion or explicit type Casting.

In Python, there are two types of Type Conversion.

  1. Implicit Conversion
  2. Explicit Conversion

You can learn these typecasting in detail from here: Type Casting in Python.



Output :

Enter kilometers
 12
7.452 miles

Python program for conversion of Miles to Kilometers (KMS)

print("Enter miles")
miles = float(input())
kms= 1.609 * miles
print(kms,"kms")

In the above program, we convert our input from string to float data type instead of integer data type. This conversion is also known as explicit conversion.



Output :

Enter miles
7.452
11.990268 kms

You can also visit:

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Python Online Quiz

Level up your coding skills with our interactive programming quiz!