Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 39 ยท Getting Started

Using Python for Physics

Use the runnable Python editors in this course to calculate, tabulate and plot physical quantities, with NumPy arrays and matplotlib graphs for motion.

Every code box in this course runs in your browser. You do not need to install anything. Python is useful in physics because it does repetitive arithmetic without mistakes, makes tables in seconds and draws graphs, which are the best way to see how quantities relate. You do not have to be a programmer: the code stays short and every program is explained.

Variables, units and printing

A variable stores a number. Python does not track units, so write them in the variable name or in a comment, and print them in the output.

Python A first physics calculation

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 f"..." strings insert values into text. The part :.2f shows two digits after the decimal point.

Lists of numbers with NumPy

Physics often needs many values at once, for example the position at every second. NumPy arrays hold a whole list of numbers and apply an operation to all of them together.

np.linspace(0, 5, 6) makes 6 evenly spaced numbers from 0 to 5. Then x = 3 * t computes x for every t at once.

Python A table of positions

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.

Plotting a graph

A position-time graph puts time on the horizontal axis and position on the vertical axis. Always label axes with the quantity and the unit.

Python Plot position against time

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.

On this site, call plt.show() and the figure appears under the code. Matplotlib is loaded for you the first time you import it, which takes a few seconds.

Functions: reuse a formula

When you use a formula more than once, wrap it in a function. It takes inputs and returns a result.

Python A reusable formula

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.

Notice that doubling the speed (second row against the first) multiplies the energy by four. Because v is squared, patterns like this become obvious in a table.

Python reading habits

Python Meaning
x**2 x squared
np.sqrt(x) square root
np.sin(a) sine, with a in radians
np.radians(30) convert 30 degrees to radians
1e-3 0.001 (scientific notation)

NumPy trig functions expect radians, not degrees. Forgetting np.radians(...) is the most common mistake in projectile problems later on.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. What does np.linspace(0, 10, 5) produce?

    linspace(start, stop, count) returns count evenly spaced numbers including both ends.
  2. 2. Which line correctly converts 30 degrees for np.sin?

    NumPy trigonometric functions take radians, so convert degrees first.
  3. 3. Why should graph axes have labels with units?

    Labels tell the reader what is plotted and in which unit.

Summary

  • The code editors run Python in your browser; no installation needed.
  • Put units in names, comments and printed output because Python does not track them.
  • NumPy arrays compute many values at once; matplotlib plots them.
  • Use functions to reuse formulas and use radians with trig functions.

Next: SI Units and Prefixes