In scientific and mathematical calculations, universal constants such as π (pi) or the gravitational constant G are frequently needed. Whether it's determining the area of a circle or calculating gravitational force, having reliable constants readily available helps streamline the process.
Scipy.constants module in SciPy allows users to access collection of mathematical, physical and unit conversion constants effortlessly with a single line of code.
Accessing Constants
Constants can be accessed using the format:
scipy.constants.CONSTANT_NAME
Below listed constants are commonly used:
Python
import scipy.constants as const
print("Pi:", const.pi)
print("Golden ratio:", const.golden_ratio)
print("Speed of light:", const.c)
print("Gravitational constant:", const.G)
print("Gas constant (R):", const.R)
print("Boltzmann constant:", const.k)
print("Proton mass:", const.proton_mass)
Output
Accessing Constants OutputFinding Constants
Use the find() method to search for constants by keyword.
Python
import scipy
res = scipy.constants.find("electron")
print(res, end='\n')
Output
Finding Constants OutputPhysical Constants: Value, Unit and Uncertainty
Not only the value but also the corresponding unit and the degree of uncertainty of a physical constant can be retrieved using physical_constants dictionary from scipy.constants.
scipy.constants.physical_constants['name']
Python
import scipy.constants as const
# This return a tuple(vslue, unit, uncertainty)
print(const.physical_constants['alpha particle mass'])
Output
(6.644657345e-27, 'kg', 2.1e-36)
Example: Sample Use Cases
Python
import scipy.constants as const
# Area of a circle using pi
def area_of_circle(r):
return const.pi * r * r
# Gravitational force
def force_gravity(M, m, dist):
return (const.G * M * m) / (dist ** 2)
print("Area of Circle:", area_of_circle(5))
print("Gravitational Force:", force_gravity(10, 5, 1))
Output
Sample Use Case OutputCommonly Used Constants in SciPy
Constants | Description |
---|
pi | Mathematical constant π |
golden_ratio | Golden ratio |
c or speed_of_light | Speed of light in vacuum |
G | Gravitational constant |
E | Elementary charge |
R | Molar gas constant |
alpha | Fine-structure constant |
N_A | Avogadro constant |
k | Boltzmann constant |
sigma | Stefan-Boltzmann constant σ |
m_e | Electron mass |
m_p | Proton mass |
m_n | Neutron Mass |
h or Planck | Planck Constant |
Unit Conversion Constants
Mass Units:
Unit | Description |
---|
gram | One gram in kilograms. |
grain | One grain in kilograms. |
pound | One Pound in kilograms. |
ounce | One Ounce in kilograms. |
automic_mass | Atomics mass constant in kilograms. |
Time Units:
Unit | Description |
---|
minute | One minute in seconds. |
hour | One hour in seconds. |
day | One day in seconds. |
year | One year in seconds. |
Length Units:
Units | Description |
---|
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. |
Pressure Units:
Units | Description |
---|
atm/atmosphere | The standard atmosphere in pascals. |
bar | One bar in Pascals. |
torr | One torr(mmHg) in pascals. |
Area Units:
Units | Description |
---|
hectare | One hectare in square meters. |
acre | One acre in square meters. |
Speed Units:
Units | Description |
---|
kmh | Kilometer per hour in meter per second. |
mph | Miles per hour in meter per second. |
mach | One Match in meter per second. |
Related Articles:
Similar Reads
Rust - Constants Constants are the value that can not be changed after assigning them. If we created a constant, then there is no way of changing its value. The keyword for declaring constant is const. In Rust, constants must be explicitly typed. Â The below syntax is used to initialize a constant value: Syntax : con
1 min read
Constants in LISP In LISP all constants are global variables. Values of constant never change throughout the execution of the program. Defining constant in LISP: New global constants are defined using the DEFCONSTANT construct Syntax: (defconstant name initial-value-form "documentation-string") Example: Let's create
2 min read
Constants in Physics Constants in Physics are fundamental values that remain unchanged across different contexts and experiments. These constants are universal in nature and are independent of the unit system used. They are essential for verifying the accuracy of theories and enabling practical applications based on tho
7 min read
Python Constant In Python, constants are variables whose values are intended to remain unchanged throughout a program. They are typically defined using uppercase letters to signify their fixed nature, often with words separated by underscores (e.g., MAX_LIMIT). Let's understand with the help of example:Python # Mat
2 min read
PL/SQL Constants In Oracle's PL/SQL (Procedural Language/SQL), constants play an important role in maintaining the integrity and predictability of the values used throughout the program. Declaring the constants enhances the program readability and reduces the errors, it also provides the performance benefits in cert
5 min read
Dart - Constants In Dart language, Constants are objects whose values cannot be changed during the execution of the program. Hence, they are a type of immutable object. A constant cannot be reassigned any value if a value has been assigned to it earlier. If we try to reassign any value to a constant, Dart throws an
2 min read