Learning Scientific Programming with Python (2nd edition)

P2.3.2: String formatting and physical constants

Question P2.3.2

The table below gives the names, symbols, values, uncertainties and units of some physical constants.

NameSymbolValueUncertaintyUnits
Boltzmann constant$k_\mathrm{B}$$1.3806504\times10^{-23}$$2.4\times 10^{-29}$$\mathrm{J\,K^{-1}}$
Speed of light$c$$299792458$(def)$\mathrm{m\,s^{-1}}$
Planck constant$h$$6.62606896 \times 10^{-34}$$3.3\times 10^{-41}$$\mathrm{J\,s}$
Avogadro constant$N_\mathrm{A}$$\;\;\,6.02214179 \times 10^{23}$$3\times 10^{16}$$\mathrm{mol^{-1}}$
Electron magnetic moment$\mu_e$$-9.28476377 \times 10^{-24}$$2.3\times 10^{-31}$$\mathrm{J/T}$
Gravitational constant$G$$\;\;\,6.67428 \times 10^{-11}$$6.7 \times 10^{-15}$$\mathrm{N\,m^2\,kg^{-2}}$

Defining variables of the form:

kB = 1.3806504e-23  # J/K
kB_unc = 2.4e-29    # uncertainty
kB_units = 'J/K'

use the string object's format method to produce the following output:

(a)

kB = 1.381e-23 J/K

(b)

G = 0.0000000000667428 Nm^2/kg^2

(c) Using the same format specifier for each line,

kB   =  1.3807e-23 J/K
mu_e = -9.2848e-24 J/T
N_A  =  6.0221e+23 mol-1
c    =  2.9979e+08 m/s

(d) Again, using the same format specifier for each line,

===    G = +6.67E-11 [N m2 kg-2]    ===
===   μe = -9.28E-24 [      J/T]    ===

Hint: The Unicode codepoint for, μ, the lower case greek letter mu, is U+03BC.

(e) (Harder). Produce the output below, in which the uncertainty (one standard deviation) in the value of each constant is expressed as a number in parentheses relative the the preceding digits: that is, $6.62606896(33) \times 10^{-34}$ means $6.62606896 \times 10^{-34} \pm 3.3 \times 10^{-41}$.

G = 6.67428(67)e-11 Nm2/kg2
mu_e = -9.28476377(23)e-24 J/T