Python

Part 1

Introduction

Installing

Python usually comes installed by default on Linux machines. But, if not, on Ubutnu we would run

$ sudo apt install python3 python-is-python3

First “Program”

Demo: Hello World

demos/02-helloworld.sh

Part 2

Python as a Calculator

The force of gravity between two masses is given by \[ F = \frac{G m_1 m_2}{r^2} \]

What will the gravitational force between a 100 kg and a 200 kg mass placed 0.1 meter apart?

Recall that \(G = 6.67430\times 10^{-11} \text{m}^3 \text{kg}^{-1} \text{s}^{-2}\)

demos/03-calculator.sh

Part 3

Python as a Calculator

Variables

What will the gravitational force between a 500 kg and a 200 kg mass placed 0.01 meter apart?

. . .

Variables in Python are different than bash:

demos/04-variables.py

Python as a Calculator

Functions

What will the gravitational force between a 5000 kg and a 2500 kg mass placed 0.2 meter apart?

. . .

Functions are different in Python than Gnuplot:

Python as a Calculator

Part 4

Python as a Calculator

Modules

The torque generated by a force is given by the cross product of the lever arm and the force \[ \vec{\tau} = \vec{r} \times \vec{F} \]

The magnitude of the torque will be \[ \tau = \left| \vec{r} \times \vec{F} \right| = r F \sin\theta \]

. . .

Python as a Calculator

Modules

./demos/06-modules.sh

Standard Library

The “Python Standard Library” is a collection of modules (like the math module) that are included with Python.

There are dozens of librarys…

https://docs.python.org/3/library/

Part 5

Useful Modules: pint

What is the gravitational force between a 200 lb mass and a 150 lb mass separated by 6 ft?

./demos/07-pint.sh

Local installs and virtual environments

Local installs and virtual environments

Steps to create and use a virtual environment:

Part 6

A better interpreter

./demos/08-ptpython.sh

Part 7

Useful Modules: pyErrorProp

pyErrorProp is a python module for doing error propagation.

Error Propagation Example

If you measure the length of a table to be 150 centimeter, plus or minus 0.5 centimeter, and the width to be 80 centimeter, plus or minus 0.5 centimeter, what is the area of the table top?

Error Propagation Example

Straight out of the Physics Lab Error Analysis Reference:

You drop a ball bearing 10 times from a height of 3 meter, plus or minus 0.5 cm, and measure the fall time to be 0.79 s, plus or minus 0.02 s. What is \(g\)? Recall: \(g = \frac{2h}{t^2}\)

Another group performs the same experiment and computes \(g = 10.6 \pm 0.4\) meter per second squared. Are your measurements consistent?

Useful Modules: numpy

Numerical Python (numpy) is a (large) collection of tools for doing numerical calculations in python.

For us, it provides tools for doing vector operations (compute the dot product, cross product, etc), matrix-vector operations (multiply a vector by a matrix), and matrix operations (invert matrix, compute eigen values and eigen vectors, etc)

Useful Modules: numpy

Vector operations

  1. How much work is done by a force \(\vec{F} = 5 N \hat{x}\) if the object that the force is exerted on is displaced \(\Delta \vec{r} = 2 m \hat{x} + 4 m \hat{y}\).

    Recall: \(W = \vec{F} \cdot \Delta\vec{r}\)

  2. Compute the torque generated by a for \(\vec{F} = 5 N \hat{x}\) exerted on a lever arm \(r = 10 cm \hat{x} + 10 cm \hat{y}\)

    Recall: \(\vec{\tau} = \vec{r} \times \vec{F}\)

    Also: \(\tau = r F \sin\theta\)

Useful Modules: numpy

Work Calculation

Useful Modules: numpy

Torque Calculation

Useful Modules: numpy

Matrix-vector operations

Matrix-vector multiplication is as simple as creating a matrix and “dotting” it with a vector.

Useful Modules: numpy

Matrix operations

Systems of equations can be represented as matrix equations.

  1. Jack goes out for a run and plans to run 2 miles. He starts out running at a brisk 10 mile/hour pace, but soon tires and ends up walking the rest of the way at 2 mile per hour. It takes Jack 45 minutes to travel the two miles. At what time did Jack stop running?

Useful Modules: numpy

System of Equations

  1. Jack goes out for a run and plans to run 2 miles. He starts out running at a brisk 10 mile/hour pace, but soon tires and ends up walking the rest of the way at 2 mile per hour. It takes Jack 45 minutes to travel the two miles. At what time did Jack stop running?

Useful Modules: numpy

System of Equations

  1. Jack goes out for a run and plans to run 2 miles. He starts out running at a brisk 10 mile/hour pace, but soon tires and ends up walking the rest of the way at 2 mile per hour. It takes Jack 45 minutes to travel the two miles. At what time did Jack stop running?

Useful Modules: numpy

System of Equations

  1. Jack goes out for a run and plans to run 2 miles. He starts out running at a brisk 10 mile/hour pace, but soon tires and ends up walking the rest of the way at 2 mile per hour. It takes Jack 45 minutes to travel the two miles. At what time did Jack stop running?

Useful Modules: numpy

System of Equations

  1. Jack goes out for a run and plans to run 2 miles. He starts out running at a brisk 10 mile/hour pace, but soon tires and ends up walking the rest of the way at 2 mile per hour. It takes Jack 45 minutes to travel the two miles. At what time did Jack stop running?