Physical constants (speed of light, Planck) in SciPy - Time & Space Complexity
We want to see how the time to get physical constants changes as we ask for more constants.
How does the work grow when we retrieve many constants like speed of light or Planck's constant?
Analyze the time complexity of the following code snippet.
from scipy.constants import physical_constants
constants_to_get = ["speed of light in vacuum", "Planck constant", "electron mass"]
results = []
for name in constants_to_get:
value = physical_constants[name]
results.append(value)
This code fetches values for a list of physical constants one by one from scipy.
- Primary operation: Looping over the list of constant names and fetching each constant.
- How many times: Once for each constant in the list.
Each new constant adds one more fetch operation, so the work grows directly with the number of constants.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 fetches |
| 100 | 100 fetches |
| 1000 | 1000 fetches |
Pattern observation: The time grows in a straight line as we ask for more constants.
Time Complexity: O(n)
This means the time to get constants grows directly with how many constants you ask for.
[X] Wrong: "Fetching multiple constants is instant and does not depend on how many I ask for."
[OK] Correct: Each constant requires a separate lookup, so more constants mean more work and more time.
Understanding how work grows with input size helps you explain efficiency clearly and shows you can think about performance in real tasks.
"What if we stored all constants in a dictionary once and then accessed them repeatedly? How would the time complexity change?"
Practice
c in scipy.constants measured in?Solution
Step 1: Understand the unit of speed of light in scipy.constants
The speed of lightcinscipy.constantsis given in meters per second (m/s), which is the standard SI unit for speed.Step 2: Confirm the unit from official documentation or usage
Checking the official scipy documentation confirmscis in meters per second.Final Answer:
Meters per second -> Option AQuick Check:
Speed of light unit = meters per second [OK]
- Confusing meters per second with kilometers per hour
- Assuming miles or feet units
- Not checking scipy documentation
Solution
Step 1: Recall the exact attribute name for Planck's constant
In scipy.constants, Planck's constant is accessed asplanck(all lowercase).Step 2: Check import syntax correctness
import scipy.constants; scipy.constants.planck usesimport scipy.constantsand accessesscipy.constants.planck, which is correct. from scipy.constants import planck tries to importplanckdirectly, which is valid but not the exact syntax shown here. import scipy.constants as sc; sc.Planck uses incorrect capitalizationPlanck. from scipy.constants import Planck also uses wrong capitalization and import style.Final Answer:
import scipy.constants; scipy.constants.planck -> Option AQuick Check:
Correct attribute name is planck, accessed via scipy.constants [OK]
- Using uppercase 'Planck' instead of lowercase
- Incorrect import syntax
- Trying to import planck directly without correct syntax
from scipy.constants import c, planck wavelength = 500e-9 # 500 nm in meters energy = planck * c / wavelength print(round(energy, 20))
Solution
Step 1: Calculate photon energy using formula E = h * c / wavelength
Given wavelength = 500 nm = 500e-9 m, Planck's constant (h) and speed of light (c) are constants from scipy.constants. Calculate energy = planck * c / wavelength.Step 2: Perform calculation and round result
Using scipy.constants values: planck ≈ 6.62607015e-34, c ≈ 299792458, energy ≈ (6.62607015e-34 * 299792458) / 500e-9 = 3.972891714285714e-19 J. Rounded to 20 decimals matches 3.972891714285714e-19.Final Answer:
3.972891714285714e-19 -> Option DQuick Check:
Energy = h*c/wavelength = 3.97e-19 J [OK]
- Forgetting to convert nm to meters
- Mixing up multiplication and division
- Rounding too early or incorrectly
from scipy.constants import Planck, c wavelength = 600e-9 energy = Planck * c / wavelength print(energy)
Solution
Step 1: Check import statement for Planck constant
The code importsPlanckwith uppercase 'P', but in scipy.constants it is lowercaseplanck. This causes an AttributeError.Step 2: Verify other parts of the code
Wavelength is correctly converted to meters (600e-9), speed of lightcis imported correctly, and the formula uses correct division. So no other errors.Final Answer:
Planck constant is incorrectly capitalized -> Option CQuick Check:
Correct attribute is 'planck' lowercase [OK]
- Using uppercase 'Planck' instead of lowercase
- Confusing units of wavelength
- Wrong operator in formula
Solution
Step 1: Calculate photon energy in joules using E = h*c / wavelength
Use wavelength in meters (400e-9). Calculate energy_joule = planck * c / wavelength.Step 2: Convert energy from joules to electronvolts
Divide energy_joule by electron_volt constant to get energy in eV.Step 3: Check code correctness
from scipy.constants import c, planck, electron_volt wavelength = 400e-9 energy_joule = planck * c / wavelength energy_ev = energy_joule / electron_volt print(round(energy_ev, 3)) correctly imports electron_volt, converts units properly, and prints rounded value. Other options either miss conversion, use wrong wavelength units, or wrong formula.Final Answer:
from scipy.constants import c, planck, electron_volt wavelength = 400e-9 energy_joule = planck * c / wavelength energy_ev = energy_joule / electron_volt print(round(energy_ev, 3)) -> Option BQuick Check:
Energy (eV) = (h*c/wavelength)/electron_volt [OK]
- Skipping conversion from joules to eV
- Using wavelength in nm without converting to meters
- Multiplying instead of dividing for energy calculation
Ready to prove it?
Pass the test and this pattern moves unknown
