Python Program to Convert Kilometers to Miles

P

In this program, you’ll learn to convert kilometers to miles and display it.

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

  1. WHAT IS PYTHON? THINGS TO KNOW BEFORE CODE WITH PYTHON
  2. HOW DO I GET AND INSTALL PYTHON?
  3. PYTHON PRIMITIVES – VARIABLES, BUILT-IN DATA TYPES, COMMENTS, SYNTAX, AND SEMANTICS
  4. CODING APPROACHES IN PYTHON
  5. STYLE GUIDE FOR PROGRAMMING PYTHON CODE
  6. ERRORS AND EXCEPTIONS IN PYTHON

Source Code

kilometers = 5.5

# To take kilometers from the user, uncomment the code below
kilometers = input("Enter value in kilometers")

# conversion factor
conv_fac = 0.621371

# calculate miles
miles = kilometers * conv_fac
print('%0.3f kilometers is equal to %0.3f miles' %(kilometers,miles))
Output

5.500 kilometres is equal to 3.418 miles

Note: To test the program, change the value of kilometers.

Your turn: Modify the above program to convert miles to kilometers using the following formula and run it.

kilometers = miles / conv_fac

About the author

Pardeep Patel
Pardeep Patel

Add comment

Basics of Python