Scientific Computing Using Python - PHYS:4905 - Fall 2018

Lecture #01 - 8/21/2018 - Prof. Kaaret


Introduction

The website for the class is https://homepage.physics.uiowa.edu/~pkaaret/2018f_p4905/index.html

The syllabus is at https://homepage.physics.uiowa.edu/~pkaaret/2018f_p4905/syllabus.html

The required textbook is  the Updated edition of A Student's Guide to Python for Physical Modeling by Kinder and Nelson.

Python 2.7 is installed on the computer in front of you.  Please log in now, it can take a while.  If you prefer to use your own computer, install Anaconda Python 2.7 from https://www.anaconda.com/download/  Be sure to download Python 2.7 and not Python 3.6.  We will use Python with numpy, matplotlib, ipython, and spyder.  For the this class, we'll use the desktops.  At the end of class, we can go over installing Python for anyone who wants to do that.


Algorithms and Data

What do you do when you program a computer?

Usually, programming is described as writing down a set of instructions for the computer to execute.  That is correct, but leaves out an equally important part of programming, which is thinking up appropriate data structures.  Say you want to calculate the electric field between two parallel plates.  What sort of data structure do you need?

See the source image


The textbook refers to this as 'the state' of the computer.  At a fundamental level, programs read and modify the contents of memory cells in the computer.  Fortunately, programming languages like Python abstract away from these low level details and allow us to think in terms of data structures like vectors and matrices.


The meaning of =

On your computer, startup Spyder by going to Windows Menu, Anaconda, then Spyder.  We will be using Spyder a lot, so before you might want to right click on it and do "Pin to taskbar".

Spyder has several panes.  For today, we'll be using the Console pane.  In the Console pane, type
x = 2
x = x+1
x

Most programming languages use the = sign as an assignment operator.  The variable named on the left side of the = is set equal to the value of the expression on the right side of the =.  It is ok to have the same variable on both sides of the =.  In normal math, the second line would make no sense, but in Python it means add one to the current value of x and replace the contents of x with that sum. 

If you want to check if two expressions are equal, then you need to use the operator ==.  Type
2 == 2
You should get the result True.  Type
 2 == 1
You should get the result False. 
Note that Python can use logical or Boolean values (True/False) for variables as well as numerical values. Try typing
a = True
a
The console should print out True.

Python uses two different types of numbers, integers and floating point numbers or 'floats'.  Integers do not have a fractional part.  They are often more compact to store in memory and are faster to do operations with than floats, so they are often used for efficiency.  Also, there are some situations where it doesn't make sense to use a number with a fractional part, e.g. choosing the nth entry in a vector, so Python requires use of integers in those cases.
Type
3/2
You should get the result 1.  In Python 2, when you divide two integers, you get an integer back.  This loses the fractional part of any division.  Python 3 returns a float when dividing two integers.
Python also supports complex numbers using j to represent the square root of -1.  Try
c = 4+5j

Python can also use 'strings' to represent text.  Type
s = 'Hello'
t = 'there'
s+t
You should get 'Hellothere'.  It is possible to manipulate strings, but some of the operators have different affects than for numbers.


Python 2 versus 3

Python has two major versions Python 2, currently in version 2.7, and Python 3.  The two versions of Python are not quite compatible.  The Physics computers have Python 2.7 installed.  However, Python 2 will not be maintained past 2020.  There is even a website counting down to the end of support for Python 2 at https://pythonclock.org/. The textbook is written using Python 3.  One of the biggest differences between Python 2 and 3 is how they handle division of integers.  Type
3/2
If you get 1, then you are using Python 2.  In Python 2, when you divide two integers you get an integer back.  Of course, then the division will not be very accurate in some cases.  In Python 3, when you divide two integers you get a float back.

We can make Python 2 compatible with Python 3 by including a few lines of code at the start of each session and at the top of every Python program.  The two lines are
from __future__ import division, print_function
input = raw_input

We can set up Spyder to add this automatically to any program we write using the Spyder menu. Do Tools/Preferences/Editor/Advanced and then click on 'Edit template for new modules' and add the two lines above below the last line.

We can also set up Spyder run these lines automatically in the console. Do Tools/Preferences/IPython console/Startup. Then in the box 'Lines', type the line below.
from __future__ import division, from __future__ import print_function, input = raw_input

These customizations of Spyder will also be useful when you start programming.  You will likely want to include more lines for both the console and editor after you have seen what libraries you use on a regular basis.


Modules

Python has relatively few built-in functions, but there are many libraries of useful functions written in Python and publicly available.  The most important library for us is NumPy which is a numerical processing library and implements functions like sqrt (for square root), values like pi, and data structures like vectors and matrices.  You tell Python that you want to use a library by importing it.  Type
import numpy
You should then be able to type expressions like numpy.sqrt(2) and numpy.pi.

It is inconvenient to always have to type out numpy, so Python allows you to import the numpy library and give it a different name.  Type
import numpy as np
Then try np.sqrt(2) and np.pi.  The textbook uses this as its standard way to import the numpy library and the examples in the text assume that you have previously done 'import numpy as np'.  You might consider adding this to your Sypder startup.

Some of us are very lazy and find typing even three characters too troublesome.  Python also enables this level of laziness.  Type
from numpy import *
Then try sqrt(2) and pi.  Finally, one has made Python look like the scientific programming language that it should be.  However, importing all of the functions from numpy can create issues if you or another library that you have loaded has functions with the same names.  In this case, you can limit the number of functions imported by using a line like
from numpy import sqrt, pi
In this case, Python will import only the selected functions.

 

Assignment

The remainder of today's class will be devoted to learning the basics of Python by working through the first chapter of the textbook.  As you read the chapter, enter the commands in the purple highlights into Spyder and see what you get.  When you are done with the chapter, do the first assignment which will be due at the start of class on Thursday.  If you are already comfortable with Python, feel free to move directly to the assignment.  Also, please do the reading for 8/23 before class on Thursday.

HW #1 is due at the beginning of class on Thursday, August 23.
https://homepage.physics.uiowa.edu/~pkaaret/2018f_p4905/hw01.html