Unit calculations in Python

  • Many units packages are available.
  • The pint package is demonstrated here.
    • The pint website had detailed documentation and tutorials.

Installation

  • Pint doesn't come with anaconda, so you have it install it separately.
  • Open an anaconda prompt (the black window).
  • Type the following conda install -c conda-forge pint

Summary

  • Add units to variables using * u.m, where m is meters, or some other unit.
  • 2 parts
    1. L.magnitude gives the number part
    • L.units gives the units part
  • Convert units
    • L.to(u.ft)
    • L.ito(u.ft)
    • L.to_base_units()
  • Be careful with temperature units
  • Big list of units

Import library

In [1]:
import pint              # import the library
u = pint.UnitRegistry()  # get the units from the UnitRegistry

Basic units

In [2]:
L = 5 * u.m              # variable with units of meters
t = 2 * u.s              # variable with units of time
v = L/t                  # new variable from L and t with velocity units

print(L)
print(t)
print(v)
5 meter
2 second
2.5 meter / second

Magnitude and units parts

In [3]:
print( L.magnitude )     # access the number part
print( L.units )         # access the units part
5
meter

Convert units: to

In [4]:
v_mph = v.to(u.mph)      # make a new variable in mph
v_fps = v.to(u.ft/u.s)   # make a new variable in ft/s
                         # "to" function (v is unchanged)
print(v_mph)
print(v_fps)
5.592340730136006 mph
8.202099737532809 foot / second

Convert units: ito

In [5]:
print(v)                 # v is currently in m/s

v.ito(u.ft/u.s)          # change v itself to ft/s: "ito" function
print(v)
2.5 meter / second
8.202099737532809 foot / second

Automatic units conversion

In [6]:
L1 = 5 *u.m
L2 = 7 *u.ft

print(L1 + L2)           # automatic conversion to L1's units
print(L2 + L1)           # automatic conversion to L2's units
7.1335999999999995 meter
23.404199475065617 foot

Ideal gas law: mixed unit conversion

$$\rho = \frac{MP}{R_gT}$$
In [7]:
M  = 29     * u.kg/u.kmol
P  = 1      * u.atm
T  = 600    * u.K
Rg = 62.363 * u.L*u.torr/u.mol/u.K

ρ = M*P/Rg/T

print(ρ)
print(ρ.to_base_units())    # function "to_base_units"   
print(ρ.to(u.lb/u.ft**3))   # some other desired units
0.0007750322039243355 atmosphere * kilogram * mole / kilomole / liter / torr
0.589024474982495 kilogram / meter ** 3
0.036771596702591475 pound / foot ** 3

Multiple unit names

In [21]:
E1 = 100 * u.btu        # these are all the same
E2 = 100 * u.Btu
E3 = 100 * u.BTU 

m1 = 1 * u.lb           # these are the same
m2 = 1 * u.pound

x1 = 2 * u.m            # these are the same
x2 = 2 * u.meter

x3 = 2 * u.km           # these are the same
x4 = 2 * u.kilometer    

T1 = 278 * u.K          # these are the same 
T1 = 278 * u.degK       # Note: use degR, degF, degC, not R, F, C

f1 = 10 * u.N           # these are the same
f2 = 10 * u.newton

f3 = 10 * u.force_pound # these are the same
f4 = 10 * u.lbf

Temperature

  • Best to just work with Kelvin or Rankine.
In [12]:
T = 278.15 * u.degK
print( T.to(u.degR) )
print( T.to(u.degF) )
print( T.to(u.degC) )


print( 278.15*u.degK + 41*u.degR )  # This works
# print( 278.15*u.degK + 41*u.degF) # doesn't work (offeset issue)

# T = 32 * u.degF                   # does't work (offset issue)
# print( T.to(u.degC) )             # doesn't work (offset issue)
500.6699999999999 degR
41.00000039999997 degF
5.0 degC
300.92777777777775 kelvin