Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 37 · Getting Started

Python Tools for Physics

Use Python in the browser to store physical constants, calculate with NumPy arrays and plot graphs, ready for the rest of the course.

You do not have to code to learn physics, but a few lines of Python let you explore formulas instantly: change a number, rerun, and watch what happens. Every Python box in this course runs in your browser. This lesson shows the small set of tools we reuse.

Constants in one place

Defining constants once avoids typing errors. Here are values we use later (the SI redefinition of 2019 fixed the first four exactly):

Constant Symbol Value
Boltzmann constant k_B 1.380649 × 10⁻²³ J/K
Avogadro constant N_A 6.02214076 × 10²³ mol⁻¹
Planck constant h 6.62607015 × 10⁻³⁴ J·s
Speed of light c 299,792,458 m/s
Elementary charge e 1.602176634 × 10⁻¹⁹ C
Python A constants block

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The molar gas constant R = k_B N_A comes out as about 8.314 J/(mol·K), a number we will use constantly.

Functions for formulas

Wrap a formula in a function and give it clear names and units in comments:

Python
def kinetic_energy(m, v):
    """m in kg, v in m/s, returns joules"""
    return 0.5 * m * v**2

NumPy arrays

A NumPy array applies a calculation to many numbers at once, which is perfect for tables of values.

Python A table of kinetic energies

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Doubling the speed multiplies the kinetic energy by 4, because energy depends on v².

Plotting

Matplotlib draws graphs. A good physics graph has labeled axes with units.

Python Plot KE against speed

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The plot is a curve, not a straight line, because KE ∝ v². When a graph's shape surprises you, ask what formula would produce it.

Good habits

  • Put the units in variable names or comments (speed_ms, mass_kg).
  • Print results with sensible rounding using f-strings such as {x:.3f}.
  • Check each result for plausibility. A car's kinetic energy of 10⁻⁶ J is a red flag.
  • Remember: simulations apply formulas we give them. They illustrate physics but do not replace experiments.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. In Python, 6.02214076e23 means

    The e notation means 'times ten to the power', so it is scientific notation.
  2. 2. Why use a NumPy array in a table of values?

    Array arithmetic is applied element by element, so one line gives a whole column.
  3. 3. The molar gas constant R equals

    R = k_B × N_A ≈ 8.314 J/(mol·K).
  4. 4. What should every physics graph include?

    Without labels and units, a graph cannot be interpreted.

Summary

  • Store constants once, with units in comments.
  • Use functions for formulas and NumPy arrays for tables.
  • Plot with labeled axes; ask why the curve has its shape.
  • Code applies formulas; it does not replace measurement.

Next: Temperature and Thermometers