This is a course about applying computer programming to problems of modeling physical systems and analysing data related to those systems. We'll be using a programming language called "Python 3". The first project is a "getting started" experience where you start to use the programming environment, become familiar with the basic elements of the language and, more or less, "learn your way around." Let's get started!
Before we get too far, first click on the "Help" menu and choose "User Interface Tour" and then "Keyboard Shortcuts". Look over these and remember where they are if you get stuck. You'll also see that under the "Help" menu there are links to module specific help sites for all the major python packages we'll be using this semester.
First things first. Python comes with many "built in" types. These are things like integers, floating point numbers, srings, tuples, lists and dictionaries. Let's go over those one at a time.
Integers and floats are easy:
In [1]:
#
# When the cursor is in this cell hit "shift enter" to execute the python code here
#
x=3 # x is assigned an integer value of 3
y=2.4 # y is assigned a floating point value of 2.4
print("x and y are:", x, 'and',y)
Like most programming languages you can operate on these values using unary and binary operators, like so:
In [2]:
z=x+y
print("The value of z is:", z)
These guys are used to track collections of things. For example you can have a list of integers like so:
In [3]:
aList = [1,2,9,4,7]
print("We have a list:", aList, "with a length of:", len(aList))
aTuple = (1,2,9,4,7)
print("We have a tuple:", aTuple, "with a length of:", len(aTuple))
The difference between these guys is that a list is 'mutable' and a tuple is not. In other words you can change a list by inserting, appending and deleting elements of the list, but a tuple, once born, cannot be modified.
In [4]:
aList.append(17)
print("We now have a modified list:", aList, "with a length of ", len(aList))
In [5]:
aTuple.append(12) # this will fail! You can't append to a tuple.
Items within Tuples and lists can be accessed using their "index". Note that index values start at "0":
In [6]:
print("element 3 of the list is", aList[3])
In [7]:
print("element 27 of the tuple is", aTuple[27]) # this will fail! There is no such element.
Note: When you "multiply" a list or a tuple by an integer you get copies of the original:
In [8]:
print(aList*3)
print(aTuple*4)
A string is a collection of characters, which like a list can be indexed, but has extra methods that only make sense for strings. We use string to manage textual data.
In [9]:
aString = "hello there world!"
print("We have a string:", aString, "whose length is:", len(aString))
print("the 9th element of aString is", aString[9])
You can manipulate strings by using built-in methods of string objects like so:
In [10]:
print(aString.split()) # split a string on spaces, convert to a list
In [11]:
bString="""1,2,3,4
5,6,7,8
9,10,11,12
"""
print("Here's what bString looks like:", repr(bString))
print("Let's split the lines:", bString.splitlines())
In [12]:
print(aString.upper())
A dictionary is a structured type, similar a list in that it can be indexed, but not ordered like a list:
In [13]:
aDict = {'a':1, 'b':"sam", 'joe':3.1415927}
print("We have a dictionary with keys:", aDict.keys())
print("It as values:", aDict.values())
print("And items:", aDict.items())
print("You can index it like an array:", aDict['joe'])
While you can do a lot with built-in python types, for many things we have to do in scientific computing it makes sense to import additional functionality using an "import" statement. Let's import the Pandas libaray using the python import statment:
In [14]:
# tell the plotting system that we want plots inside the notebook
%matplotlib inline
import pandas as pd # we're renaming pandas as 'pd' here to save typing
The most important object defined in pandas is the DataFrame. Here's one way to create a DataFrame with pandas using a dictionary:
In [15]:
myDF = pd.DataFrame({'x':[1,2,3,4,5], 'y':[9,8,7,6,5]})
This creates a DataFrame with two "Columns" x and y. You can fetch the columns individually:
In [16]:
print("Here is x:", myDF.x.values)
print("Here is y:", myDF.y.values)
In [17]:
#
# It is possible to plot directly from the DataFrame
#
myDF.plot('x','y')
Out[17]:
It's hard to do much without loops sooner or later! There are two basic types of loops in python "for" and "while". The while loop executes the statements within it's scope until a logical expression becomes False. A for loop executes the statements within it's scope for every element of a sequence (e.g., a list, tuple, or any other sequence type). Basically if you have a sequence you need to iterate over, use a for loop. On the other hand, if you want to iterate until some condition is met, then use a while.
Here's an example:
In [18]:
print("Starting with:", repr(bString)) # show the raw string
for s in bString.splitlines():
print("Breaking down the string:", s)
for n in s.split(','):
print("Found the item:", n)
In [19]:
#
# another way with the while loop
#
cList=bString.splitlines()
while len(cList)>0:
s=cList.pop(0) # pop off the zeroth element of the list
for n in s.split(','):
print("Found item:", n)
In [20]:
def myFunction(x): # function to compute 1.0/(x**2 + 1.0)
aLocalVar = x*x+1.0 # this is x**2 + 1.0
return 1.0/aLocalVar # finally 1.0/(x**2 + 1.0)
for i in range(10):
print("i=",i,"myFunction(i):", myFunction(i))
In [ ]: