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:
Python String format()
Python Program to Illustrate Different Set Operations
Java Program to Implement the Program Used in grep/egrep/fgrep
Python Program to Find ASCII Value of Character
Python Custom Exceptions
Python List remove()
Python Set difference_update()
Python Program to Create a Countdown Timer
Python compile()
Python Dictionary values()
Python repr()
Python Closures
Python Generators
Python Program to Convert Decimal to Binary Using Recursion
Python Set pop()
Python issubclass()
Python String isalpha()
How to get current date and time in Python?
Python List insert()
Python String rindex()
Python help()
Python Program to Differentiate Between del, remove, and pop on a List
Python Program to Display Fibonacci Sequence Using Recursion
Python String title()
Python Program to Safely Create a Nested Directory
Python range()
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Program to Check Whether a String is Palindrome or Not
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python String isspace()
Python map()
Python Keywords and Identifiers