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 Program to Capitalize the First Character of a String
Python Set issubset()
Python String lower()
Python Exception Handling Using try, except and finally statement
Python hex()
Python Data Types
Python String isdecimal()
Python Program to Find Armstrong Number in an Interval
Python sleep()
Python ascii()
Python Program to Find Factorial of Number Using Recursion
Python max()
Python Keywords and Identifiers
Python Program to Sort Words in Alphabetic Order
Python Program to Print all Prime Numbers in an Interval
Python Program to Count the Number of Digits Present In a Number
Python Dictionary clear()
Python Program to Represent enum
Python Program to Multiply Two Matrices
Python String isdigit()
Python Program to Merge Two Dictionaries
Python Program to Reverse a Number
Python object()
Python Program to Concatenate Two Lists
Python Machine Learning Eqution Reference - Sebastian Raschka
Python String partition()
Python String isidentifier()
Python Program to Check the File Size
Python list()
Python Program to Return Multiple Values From a Function
Python Dictionary pop()
Python Set intersection_update()