Python Program to convert kilometers to miles

In this basic python program, we convert the kilometers to miles. It's a beginner-level python program.

To understand this example, you should have knowledge of the following Python programming topics:

How to convert kilometers to miles in python?

In this Python program example, we have to convert kilometers to miles. To solve this problem, we need to know many kilometers are miles. 1 kilometre = 0.621 mile. So what we do in the program is to multiply the kilometers with the constant. 0.621. To convert from miles to kilometers, we will divide miles with 0.621. Let us break the code step by step.

ALGORITHM

STEP 1: Enter the kilometers that you want to convert from kilometers to miles. We use the input function in python programming to accept the string and convert that to float using float data type before saving it to a variable.

STEP 2: Enter the constant '0.621371' to convert the kilometers to miles.

STEP 3: Apply the formula to convert kilometers to miles by multiplying kilometers with a constant factor.

STEP 4: Print the result using 0.3f float; 0.3 float data type is used to get the result with 3 digit precision in python language.

Python Source Code

                                          kilometres = float(input('How many kilometres?: '))   

# conversion factor c 
f = 0.621371

miles = kilometres * f  

print('%0.3f kilometres is equal to %0.3f miles' %(kilometres,miles))

                                      

OUTPUT

How many kilometres? : 10

6.214