Package: unitspy
units.pyUnits.py is a Python library that makes it easy to work with units in physics without having to worry about unit conversions. For example, if you have an energy in electronvolts and you have the value of Planck's constant in Joules times seconds, normally you would have to convert one of those values to match the units of the other one. But with units.py, you don't have to do that. You can easily multiply two values in different units and get the correct result.SetupUnits.py can be installed using pip:pip install unitspyYou can then import it into your Python script:import unitspy as upUsageIntroductionUnits.py creates aQuantityclass. AQuantityobject represents a physical quantitiy, for example a length, a time or a mass. To create aQuantityobject, simply multiply a number by a unit provided by units.py. For example, to create a quantity representing 3 meters, do this:threeMeters = 3 * up.mYou can multiply and divideQuantityobjects just like numbers, and you can add, subtract and compare twoQuantityobjects as long as they have compatible units (for example seconds and days are compatible units as they're both units of time, seconds and meters are not as they measure different things). Example:myLength = 10 * up.m
myTime = 5 * up.s
print(myLength / myTime)    #Result: 2m/sUnitsMost units are available, and their names are often their SI abbreviations. Here is a list of the most common units:Units.py nameUnitgGramkgKilogrammMeterÅorAngstrom(the two are equivalent)ÅngströmauAstronomical unitlyLight yearpcParsecsSecondminuteMinutehrHourdDayyYearAAmpereKKelvinHzHertzNNewtonPaPascalJJouleeVElectronvoltWWattCCoulombVVoltFFaradOhmOhmTTeslaYou can also add SI prefixes before some of these units to get multiples of them. For example, writecmfor centimeter ormsfor millisecond. For units starting with "micro", you can either precede the unit withµ(for exampleµm), or write out the the name of the entire unit (for examplemicrometer).You can also multiply, divide and take the power of units. For example, you can writem / sfor meters per second orm ** 2for square meters.Angles and dimensionless quantitiesUnits.py also provides adimensionlessunit in order to representdimensionless quantities. AQuantityobject with thedimensionlessunit is meant to work exactly like an object of Python's built-infloattype.For angles, there is also aradiansunit, which is simply an alias ofdimensionless, and adegreesunit, where1 * up.degreesis equal topi / 180 * up.radians. That way, you can easily specify angles in degrees, and for example pass that as an argument to trigonometric functions likenumpy.sin, for exampleimport unitspy as up
import numpy as np
print(np.sin(float(90 * up.degree)))    #Result: 1.0There is alsoup.arcminandup.arcsec, representing arcminutes and arcseconds respectively.TemperaturesUnits.py stores temperatures in Kelvins, and can't store temperatures in Celsius. However, it definesQuantityobject namedzeroCelsiusequal to the value of 0 Celsius in Kelvin, which can be used to convert between Kelvin and Celsius. If you have a temperature in Celsius, addzeroCelsiusto get the temperature in Kelvin, and if you have a temperature in Kelvin, subtractzeroCelsiusto get the temperature in Celsius. Example:temperatureInCelsius = 15 * up.K    #Define this quantity in Kelvin even though it really represents a unit in Celsius
temperatureInKelvin = temperatureInCelsius + up.zeroCelsius
print(temperatureInKelvin)    #Result: 288.15KPhysical quantitiesUnits.py also provides pre-definedQuantityobjects corresponding to physical constants. These are some common ones:Units.py name (should be preceded byup.)QuantitykBBoltzmann constanteElementary chargeGGravitational constanthPlanck constanthbarReduced Planck constantcSpeed of lightepsilon0Vacuum permittivitymu0Vacuum permeabilitymeElectron massmpProtonmassmnNeutronmasssolarMassSolar masspiPiConversions between unitsIf you want to output the value of aQuantityobject in a specific unit, you will need thetoUnitmethod. This method takes one argument, which is the unit in which you want to output the quantity. For example, here is how you output the value of one light year in meters:myDistance = 1 * up.ly
print(myDistance.toUnit(up.m))    #Result: 9460500000000000.0mQuantityobjects also have atoSiUnitsmethod which converts it toSI unitsand atoPlanckUnitsmethod which converts it toPlanck units. These methods take no arguments. Example:myDistance = 1 * up.ly
print(myDistance.toSiUnits())    #Result: 9460500000000000.0m
print(myDistance.toPlanckUnits())    #Result: 5.853346072890479e+50lₚ