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 islower()
Python Package
Python Program to Get the File Name From the File Path
Python property()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Program to Check If a String Is a Number (Float)
Python Program to Count the Occurrence of an Item in a List
Python Matrices and NumPy Arrays
Python String find()
Python String strip()
Python divmod()
Python String swapcase()
Python Program to Measure the Elapsed Time in Python
Python Operators
Python Program to Find the Size (Resolution) of a Image
Python String isidentifier()
Python Program to Create a Countdown Timer
Python Program to Check If Two Strings are Anagram
Python eval()
Python Program to Sort Words in Alphabetic Order
Python Program to Find the Factorial of a Number
Python Shallow Copy and Deep Copy
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Program to Create Pyramid Patterns
Python String rsplit()
Python Program to Get the Full Path of the Current Working Directory
Python Program to Parse a String to a Float or Int
Python abs()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Program to Convert Decimal to Binary Using Recursion
Python frozenset()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj