# Python

Python is programming language. Many computation physicists like to use python for their research because it is time efficient (write code quickly, code runs quickly). Python is also a great language for learning/teaching because it is intuitive.

For Windows computers, “Python(x,y)” is a package of software that has everything you need: https://www.youtube.com/watch?v=FC7vjuegK88

The SciPy website is the home for all the compilers, editors and functions libraries related to scientific computing with Python.

Python 2.X, when combined with the right library of functions, is like MatLab. The module that contains the Matlab-emulating functions is called “PyLab”. PyLab is free (you can easily install it on your own computer), MatLab is expensive. PyLab gets you on track for computational physics research. Lastly, Python is the language of choice for many companies. I've heard that Google requires their job applicants to know Python.

If you want to work with PyLab on your own computer:

- Download and Install Python 2.X
- Download and Install the appropriate version of NumPy (it will automatically detect Python)
- Download and Install the appropriate version of SciPy (it will automatically detect Python)

There is a known issue with Numpy and SciPy on Mac OS 10.7. See this posting.

# Learning Python

Python is used in the undergraduate computational physics courses at OSU.

- Course notes from PH265: Learning the basics from scratch.
- Course notes from PH365: Review of the basics.

Both documents were put together by Prof. David Roundy (OSU Physics).

My favorite way to run Python is through the program “Idle”. Using Idle, you import the Pylab module. Then, any instruction you type into Idle is sent to Python, along with the necessary code from the Pylab module. If you request a printed answer, or a graph, Python will send the answer back to Idle.

If you forget to import the Pylab module, typing

>sin(pi)

will give an error message. The function sin and the value pi are not accessible until you import an appropriate module.

## Examples

#### Opening the program

- Log into your account on a computer in Weniger 412
- Open the “terminal”
- Type “idle” and hit return.
- Open a new window (this will be your scripting window, where you gather together a list of instructions).

#### Example of a "while loop"

from __future__ import division from pylab import * t = arange(0, 20, 0.1) #seconds v = zeros(t.size) #meters/seconds i = 0 while i < t.size: v[i] = -9.8*t[i] i = i + 1 show(plot(v))

#### Example of a "for loop"

from __future__ import division from pylab import * t = arange(0, 20, 0.1) #seconds v = zeros(t.size) #meters/seconds for i in range(t.size): v[i] = -9.8**t[i] show(plot(v))

#### Example of a "for loop"

from __future__ import division from pylab import * t = arange(0, 20, 0.1) #seconds v = zeros(t.size) #meters/seconds for i in t: v[i] = -9.8**t[i] show(plot(v))

#### Example of plotting lines on the same graph

from __future__ import division from pylab import * x = arange(0,6,0.05) y1 = sin(x) y2 = sin(1.1*x) plot(x,y1) plot(x,y2) title('Sine waves with different periods') xlabel('y (dimensionless)') ylabel('x (dimensionless)') show()

#### Example of numerical integration

from __future__ import division from pylab import * x = arange(0,10,0.05) y = exp(-x**2) sum(y)