In this example, we’ll learn to convert kilometers to miles and display it.
To understand this example, you should have the knowledge of the following Python programming topics:
Example: Kilometers to Miles
# Taking kilometers input from the user kilometers = float(input("Enter value in kilometers: ")) # conversion factor conv_fac = 0.621371 # calculate miles miles = kilometers * conv_fac print('%0.2f kilometers is equal to %0.2f miles' %(kilometers,miles))
Output
Enter value in kilometers: 3.5 3.50 kilometers is equal to 2.17 miles
Here, the user is asked to enter kilometers. This value is stored in the kilometers variable.
Since 1 kilometer is equal to 0.621371 miles, we can get the equivalent miles by multiplying kilometers with this factor.
Your turn: Modify the above program to convert miles to kilometers using the following formula and run it.
kilometers = miles / conv_fac
Related posts:
Node.js vs Python for Backend Development
Python Program to Display Powers of 2 Using Anonymous Function
Python String rindex()
Python bool()
Python String rpartition()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python Program to Concatenate Two Lists
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Dictionary
Python String maketrans()
Introduction to Scientific Programming with Python - Joakim Sundnes
Python divmod()
Python classmethod()
Python List sort()
Python sum()
Python Dictionary copy()
Python String count()
Python strftime()
Python Program to Check if a Number is Odd or Even
Python filter()
Python Input, Output and Import
Python Program to Make a Flattened List from Nested List
Python Object Oriented Programming
Python String replace()
Python Program to Return Multiple Values From a Function
Python break and continue
Python String isnumeric()
Python slice()
Python Function Arguments
Python frozenset()
Python List pop()
Python Set copy()