SciPy – Constants
Scipy stands for Scientific Python and in any Scientific/Mathematical calculation, we often need universal constants to carry out tasks, one famous example is calculating the Area of a circle = ‘pi*r*r’ where PI = 3.14… or a more complicated one like finding forcegravity = G*M*m ⁄ (distance)2 where G = gravitational constant. In all such scenarios, it would be very handy if we have reference material to look up these constants and incorporate them into our calculation with ease.
Scipy-Constants is a sub-module inside the Scipy library that does this for us. It contains an exhaustive list of universal mathematical constants, Physical constants, and units. which can be looked up with just 1 line of code.
Just type the name of the constant in place of XXXX in ‘scipy.constants.XXXX‘ format to access its value. Below listed are a few most important constants using scipy.constant module down below. The list is not exhaustive, but it gives a good idea of how to access constants.
We can use an inbuilt method to find the constants relevant to our use case. Constants are stored using a dictionary data structure, and we can use the scipy. constants.find() API to find all relevant constants from the dict and use them accordingly.
The code below demonstrates using scipy. constants.find() API. The below code prints all constants which contain the ‘electron’ word in it, and we can filter out the one which is required.
Not just the magnitude of a constant we can also access the unit and degree of uncertainty associated with the magnitude of any physical_constants stored in scipy.constants module, using the format
physical_constants[name] = (value, unit, uncertainty).
(6.6446573357e-27, 'kg', 2e-36)
Apart from the above variables, scipy.constants also contain more physical constants, and below is a list of all methods available in scipy.constants module with an explanation.
Below are the most commonly used constants available in SciPy module:
Constants Description pi Mathematical pi value golden Mathematicalgolden ratio c Speed of light in vacuum speed_of_light Speed of light in vacuum G Standard acceleration of gravity G Newton Constant of gravitation E Elementary charge R Molar gas constant Alpha Fine-structure constant N_A Avagadro constant K Boltzmann constant Sigma Stefan-Boltzmann constant σ m_e Electron mass m_p Proton mass m_n Neutron Mass H Plank Constant Plank constant Plank constant h
Below are the unit constants available in the SciPy module:
|Gram||One gram in Kilogram.|
|Grain||One grain in Kilogram.|
|Pound||One Pound in Kilogram.|
|Ounce||One Ounce in Kilogram.|
|automic_mass||Atomics mass constant in Kilogram.|
|Minute||One minute in seconds.|
|Hour||One hour in seconds.|
|Day||One day in seconds.|
|Year||One year in seconds.|
|Inch||One inch in meters.|
|Foot||One foot in meters.|
|Yard||One yard in meters.|
|Pt||One point in meters.|
|Micron||One Micron in meters.|
|Atm||The standard atmosphere in pascals.|
|Atmosphere||The standard atmosphere in pascals.|
|Bar||One bar in Pascals.|
|Torr||One torr(mmHg) in pascals.|
|Hectare||One hectare in square meters.|
|Acre||One acre in square meters.|
|Kmh||Kilometer per hour in meter per second.|
|Mph||Miles per hour in meter per second.|
|Mach||One Match in meter per second.|