Part A: Python as a calculator

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

You can also import just 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

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 Scientific.Geometry and must be imported from it:
from Scientific.Geometry import Vector

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

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

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

In [13]: print Vector(0,1,0)-Vector(1.5,4,0)
[-1.5, -3.0, 0.0]

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

In [15]: print Vector(0,0,1)*4.
[0.0, 0.0, 4.0]

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

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

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

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

Normalization:
In [20]: print Vector(1.,4.,2.).normal()
[0.21821789023599239, 0.87287156094396956, 0.43643578047198478]

Angle between two vectors:
In [21]: print Vector(1,2.5,0).angle(Vector(0,-1,3.1))
1.85984962426
The result is in radians.

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

In [24]: print Vector(1,4.,3).normal()[1]
0.784464540553
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!)

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 Scientific.Geometry import Vector

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

print "a-b:", (a-b).length()
print "a-c:", (a-c).length()
print "b-c:", (b-c).length()

Type this into a file test.py using the %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