Units and unit conversions are BIG in engineering. Engineers solve the world's problems in teams. Any problem solved has to have a context. How heavy can a rocket be and still make it off the ground? What thickness bodypannels keep occupants save during a crash? In engineering, a number without a unit is like a fish without water. It just flops around hopelessly without context around it is useless.

How can we get help using units? Programming is one way. We are going to complete some uit conversion problmes using Python and Pint. Pint is a Python package used for unit conversions.

See the (Pint documentation) for more examples.

I recommend that undergraduate engineers use Python 3 (Python 2.7 is legacy python) and the Anaconda distribution. To use Pint, we need to install pint in our wroking version of Python. Open up the Anaconda Prompt:

> pip install pint

I am working on a Windows 10 machine. You can check your operating system and Python version using the code below:

In [1]:
import platform
print('Operating System: ' + platform.system() + platform.release())
print('Python Version: '+ platform.python_version())

Operating System: Windows10
Python Version: 3.6.3


Before we can complete a unit conversion with the Pint package, we need to import the Pint module and instantiate a UnitRegistry object. The new ureg object contains all the units used in the examples below.

In [2]:
import pint
ureg = pint.UnitRegistry()


For our first problem, we will complete the following converison:

Convert 252 kW to Btu/day¶

We'll create a variable called power with units of kilowatts (kW). To create the kW unit, we'll use our ureg object.

In [3]:
power = 252*ureg.kW
print(power)

252 kilowatt


To convert power to Btu/day, we use Pint's .to() method. The .to() method does not change the units of power in place. We need to assign the output of the .to() method to another variable power_in_Btu_per_day

In [4]:
power_in_Btu_per_day = power.to(ureg.Btu / ureg.day)
print(power_in_Btu_per_day)

20636632.5971578 btu / day


Another probem:

Convert 722 MPa to ksi¶

In [5]:
stress = 722*ureg.MPa

In [6]:
stress_in_ksi = stress.to(ureg.ksi)
print(stress_in_ksi)

104.71724664121106 kip_per_square_inch


Next problem:

Convert 1.620 m/s2 to ft/min2¶

In [7]:
accel = 1.620 *ureg.m/(ureg.s**2)
print(accel)

1.62 meter / second ** 2


This time we will use the .ito() method. Using .ito() will convert the units of accel in place.

In [8]:
accel.ito(ureg.ft/(ureg.min**2))
print(accel)

19133.85826771654 foot / minute ** 2


Convert 14.31 x 108 kJ kg mm-3 to cal lbm / in3¶

In [9]:
quant = 14.31e8 * ureg.kJ * ureg.kg * ureg.mm**(-3)
print(quant)

1431000000.0 kilogram * kilojoule / millimeter ** 3

In [10]:
quant.ito( ureg.cal*ureg.lb / (ureg.inch**3))
print(quant)

1.2356155557389996e+16 calorie * pound / inch ** 3