Template request | Bug report | Generate Data Product
Tags: #python #convert #units #snippet #operations #speed
Author: Benjamin Filly
Description: This notebook shows you how to convert speed using Python.
References:
value
: starting unit valuefrom_unit
: is the unit of the starting value, the one you want to convertto_unit
: this is the unit we want to achieve with this script
value = 50
from_unit = 'km/h'
to_unit = 'm/s'
def convert_speed(value, from_unit, to_unit):
units = {
'm/s': 1.0,
'km/h': 1 / 3.6,
'mph': 0.44704,
'knot': 0.514444
# Add other speed units here
}
if from_unit not in units or to_unit not in units:
raise ValueError("Invalid unit.")
return value * units[from_unit] / units[to_unit]
result = convert_speed(value, from_unit, to_unit)
result