conda install -c conda-forge pint
* u.m
, where m
is meters, or some other unit.L.magnitude
gives the number partL.units
gives the units partL.to(u.ft)
L.ito(u.ft)
L.to_base_units()
import pint # import the library
u = pint.UnitRegistry() # get the units from the UnitRegistry
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
print( L.magnitude ) # access the number part
print( L.units ) # access the units part
5 meter
to
¶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
ito
¶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
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
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
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
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