Convert Miles to Kilometers

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this challenge, you will write a Python function to convert miles to kilometers. You will be given a distance in miles and you need to return the equivalent distance in kilometers.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") subgraph Lab Skills python/comments -.-> lab-13692{{"`Convert Miles to Kilometers`"}} python/function_definition -.-> lab-13692{{"`Convert Miles to Kilometers`"}} end

Miles to km

Write a Python function called miles_to_km that takes a parameter miles (a float or an integer) and returns the equivalent distance in kilometers. You should use the following conversion formula: km = mi * 1.609344.

def miles_to_km(miles):
  return miles * 1.609344
miles_to_km(5.03) ## 8.09500032

Summary

In this challenge, you learned how to convert miles to kilometers using a Python function. You should now be able to take any distance in miles and convert it to kilometers using the formula km = mi * 1.609344.

Other Python Tutorials you may like