Part A: Python as a calculator

The following lab assumes you're familiar (a bit) with iPython. If not, see the Tutorials.

Numbers:

Integer 0, 1, 2, 3, -1, -2, -3
Real 0., 3.1415926, -2.05e30, 1e-4
(must contain a dot or an exponent)
Imaginary/Complex 1j, -2.5j, 3+4j
(the last one is a sum)
Addition 3+4, 42.+3, 1+0j
Subtraction 2-5, 3.-1, 3j-7.5
Multiplication 4*3, 2*3.14, 1j*3j
Division 1/3, 1./3., 5/3j
Power 1.5**3, 2j**2, 2**-0.5

Try each of these out. What does Python respond with? Do you understand the result in each case?

There are some subtleties. When the two numbers are not of the same type, the result is of the higher type in the order: integer, real, complex.

Caution: For example, 1/3 is an integer, hence 0. However, 1./3. is a real number, as expected.
In [16]: 1/3
Out[16]: 0

In [17]: 1./3.
Out[17]: 0.33333333333333331

The precision of real and complex numbers is that of the type double of the C compiler used to generate the Python interpreter. On most systems, this corresponds to a precision of about 16 decimal digits.

Mathematical functions:

The standard mathematical functions (sqrt, log, log10, exp, sin, cos, tan, arcsin, arccos, arctan, sin, cosh, plus some others to be mentioned later), as well as the constants pi and e, are not part of the basic language, but contained in a module called math. You must therefore import them before using them:

They can be imported individually,
from math import sqrt
or in groups,
from math import sin, cos, tan
or you can import everything from that module:
from math import *
Then you can type:
In [26]: print sin(pi/3)
0.866025403784

However, I strongly recommend against this general way of importing packages. The main reason being that the functions in the package might have names that overwrite the names you are using or that other packages use. This will lead to bizarre and hard to track-down errors.

Better, you can import the module:
import math
and then use extended names for the functions: math.sqrt, math.exp, and so on.
In [1]: import math

In [2]: print math.sin(3)
0.14112000806

This way it's absolutely clear to which package a function belongs.

Text strings:

There are two forms of text strings: 'abc' or "abc"
In [3]: print 'abc'
abc

In [4]: print "def"
def

Line breaks are indicated by a newline character '\n': "abc\ndef":
In [5]: print "abc\ndef"
abc
def

Concatenation is done using the '+' operator:
In [6]: print "abc"+'def'
abcdef

Repetition: A similar arithmetic operation gives repeats:
In [8]: print 8*"ab"
abababababababab

Vectors:

Vectors are not a fundamental data type in Python. They are defined in the module numpy and must be imported from it:
from numpy import *
We used this form of import to bring in some other numpy functions that we'll use.

Notation: array([1,0,0])
In [10]: array([1,0,0])
Out[10]: array([1, 0, 0])

In [11]: print array([1,0,0])
[1 0 0]

Addition and subtraction:
In [12]: print array([1,0,0])+array([0,-1,3])
[ 1 -1  3]

In [13]: print array([0,1,0])-array([1.5,4,0])
[-1.5 -3.   0. ]

Multiplication by a scalar:
In [14]: print 3.5*array([1,1,0])
[ 3.5  3.5  0. ]

In [15]: print array([0,0,1])*4.
[ 0.  0.  4.]

Division by a scalar:
In [16]: print array([1,1,0])/2.
[ 0.5  0.5  0. ]

Component-wise vector multiplication:
In [17]: print array([1,2.5,0])*array([0,-1,3.1])
[ 0.  -2.5  0. ]

Dot product:
In [17]: print sum(array([1,2.5,0])*array([0,-1,3.1]))
-2.5

Cross product:
In [18]: print cross([1,2.5,0],[0,-1,3.1])
[ 7.75 -3.1  -1.  ]
The syntax here is a bit odd, no? (We will come back to this later.)

Length:
In [19]: print sqrt(sum(array([2.5, 3.4, 1.])*array([2.5, 3.4, 1.])))
4.33704968844
Again, there's unusual syntax needed here to access the length operation.

Accessing components:
In [23]: print array([1,0,3])[0]
1

In [24]: print array([1,0,3])[2]
3
Note that the indices run from 0 to 2. The square bracket notation [....] gives pieces of the vector. This hints at some of the more sophisticated manipulations of lists—a vector is a list of numbers—that Python allows.

At a minimum, you can use Python as a vector-savvy, general purpose calculator.

Matrices will come shortly!

As well, we will introduce more of the Python numerical package numpy soon. As it turns out, array is a more general type of multidimensional array and comes with many more sophisticated operations.

Variables:

However, we would like to do more interesting calculations which often require storing temporary values. Variables are used to store and give names to values:
In [25]: x = 2.

In [26]: print x
2.0
In [27]: sum = x + 25

In [28]: print sum
27.0

In [29]: greeting = "hello"

In [30]: print greeting
hello

In [31]: a_very_special_value = 42

In [32]: print a_very_special_value
42

There are a few rules for forming variable names. Variable names can be arbitrarily long and contain letters and digits. They must begin with a letter. Upper and lower case letters are considered to be different.


A Python program

This program defines three points forming a triangle and prints the distances between all the pairs.
from numpy import *

a = array([0, 1, 0])
b = array([4.3, -2.4, 0.005])
c = array([-3.2, 5.1, -3.])

print "a-b:", sqrt(sum((a-b)*(a-b)))
print "a-c:", sqrt(sum((a-c)*(a-c)))
print "b-c:", sqrt(sum((b-c)*(b-c)))

Type this into a file test.py using iPython's %edit command
ed -x test.py
When you exit, run it
In [37]: run test
a-b: 5.48179030974
a-c: 6.00416522091
b-c: 11.0240657201

Congratulations, your first Python program!


Table of Contents