Part 1: 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

When the two numbers are not of the same type, the result is of the higher type in the order integer-real-complex.

Caution: 1/3 is an integer, hence zero. 1./3. is a real number, as expected.

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 Numeric. You must therefore import them before using them:

They can be imported individually,

from Numeric import sqrt
or in groups,
from Numeric import sin, cos, tan
or you can import everything from that module:
from Numeric import *

You can also import just the module:

import Numeric
and then use "extended" names for the functions: Numeric.sqrt, Numeric.exp etc.

Example:

from Numeric import pi, sin
print sin(pi/3)

Text strings:

Two forms: 'abc' or "abc"

Line breaks are indicated by \n: "abc\ndef"

Concatenation: "abc"+'def' gives "abcdef"

Repetition: 3*"ab" gives "ababab"

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)

Addtition, subtraction: Vector(1,0,0)+Vector(0,-1,3), Vector(,1,0)-Vector(1.5,4,0)

Multiplication by a scalar: 3.5*Vector(1,1,0), Vector(0,0,1)*4.

Division by a scalar: Vector(1,1,0)/2.

Dot product: Vector(1,2.5,0)*Vector(0,-1,3.1)

Cross product: Vector(1,2.5,0).cross(Vector(0,-1,3.1))

Length: Vector(2.5, 3.4, 1.).length()

Normalization: Vector(1.,4.,2.).normal()

Angle between two vectors: Vector(1,2.5,0).angle(Vector(0,-1,3.1)) (the result is in radians)

Accessing components: Vector(1,0,3)[0], Vector(1,4.,3).normal()[1] (indices run from 0 to 2)

Variables:

Variables are used to store and give names to values:

x = 2.
sum = x + 25
greeting = "hello"
a_very_special_value = 42

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.


Your first 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()

Exercises


Table of Contents