Python Program to Convert Kilometers to Miles

Here’s an example Python code to convert kilometers to miles:

# Get input from user for distance in kilometers
kilometers = float(input("Enter distance in kilometers: "))

# Convert kilometers to miles using the conversion factor
conversion_factor = 0.621371
miles = kilometers * conversion_factor

# Display the result
print(f"{kilometers} kilometers is equal to {miles:.2f} miles")

In this code, we first prompt the user to enter the distance in kilometers using the input() function and convert it to a floating-point number using the float() function. We then define a conversion factor of 0.621371, which represents the number of miles in one kilometer. We multiply the distance in kilometers by this conversion factor to obtain the distance in miles. Finally, we use the print() function to display the result to the user, using f-strings to format the output with two decimal places.

When you run this code, it should prompt you to enter a distance in kilometers. After you enter a number, it should convert it to miles and display the result, similar to the following:

Enter distance in kilometers: 10
10.0 kilometers is equal to 6.21 miles

Note that you can modify the conversion factor or the formatting of the output as needed.

Related Examples

1. Python Program to Print Hello world!

2. Calculate the multiplication and sum of two numbers

3. Python Program to Find the Square Root

4. Python Program to Calculate the Area of a Triangle

5. Python Program to Solve Quadratic Equation

6. Python Program to Swap Two Variables

7. Python Program to Generate a Random Number

8. Return the count of a given substring from a string

9. Print the following pattern

10. Python Program to check if the given number is a palindrome number

11. Python Program to print multiplication table form 1 to 10

12. Print downward Half-Pyramid Pattern with Star (asterisk)

13. Python Program to Check if a Number is Positive, Negative or 0

14. Python Program to Find the Sum of Natural Numbers

15. Print the sum of the current number and the previous number in Python

Leave a Reply

Your email address will not be published. Required fields are marked *

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial