Boolean Arrays Classes Computational Mathematics with Python Basics Olivier Verdier and Claus Führer 2009-03-24 Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 1 / 40
1 Python vs Other Languages Examples and Demo 2 3 Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 2 / 40
Python vs Other Languages Examples and Demo 1 Python vs Other Languages Examples and Demo 2 3 Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 3 / 40
Python vs Other Languages Examples and Demo Why Python? Python is... Free and open source It is a scripting language, meaning that it is interpreted It is modern: object oriented, exception handling, dynamic typing etc. Plenty of libraries, in particular scientific ones: linear algebra; visualisation tools: plotting, image analysis; differential equations solving; symbolic computations; statistics ; etc. Many possible usages: Scientific computing (of course :-)), scripting, web sites, text parsing, etc. Used by YouTube, Google, NASA, Los Alamos, NSA among others Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 4 / 40
Python vs language XX Python vs Other Languages Examples and Demo Java, C++ Object oriented compiled languages. Very limited and extremely verbose. Low level compared to python. Few scientific libraries. C, FORTRAN Very low level compiled language. Useful in some CPU critical situations. php, ruby Other interpreted languages. PHP is web oriented. Ruby is as flexible as python but has no scientific library. MATLAB Tool for matrix computation that evolved for scientific computing. The scientific library is huge but it is not a programming language. Extremely expensive. Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 5 / 40
Examples Python vs Other Languages Examples and Demo Python may be used in interactive mode: >>> x = 3 >>> y = 5 >>> print x + y 8 Here we solve [ ] 1 2 x = 3 4 >>> M = array ([[1., 2.], [3., 4.]]) >>> V = array ([2., 1.]) >>> x = solve (M, V) >>> print x [-3. 2.5] [ ] 2 1 Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 6 / 40
More examples Python vs Other Languages Examples and Demo Computing e iπ and 2 100 : >>> print exp ( 1j* pi) # should return - 1 : -) (-1+1. 22464679915e - 16j ) >>> print 2** 100 1267650600228229401496703205376L Computing ζ(x) = k=1 1 k x. For x = 2 we know that ζ(2) = π2 6 : # for x = 2: >>> print scipy. special. zeta (2., 1) 1. 64493406685 >>> print pi**2/6 1. 6449340668482264 Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 7 / 40
Demo Python vs Other Languages Examples and Demo Demo Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 8 / 40
1 Python vs Other Languages Examples and Demo 2 3 Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 9 / 40
Numbers A number may be an integer, a real number or a complex number. The usual operations are + and - addition and substraction * and / multiplication and division ** power 2**(2+2) # 16 1j**2 # -1 Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 10 / 40
Strings Strings are lists of characters, enclosed by simple or double quotes: valid string " string with double quotes " Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 11 / 40
Strings Strings are lists of characters, enclosed by simple or double quotes: valid string " string with double quotes " You may also use triple quotes for strings including multiple lines: """ This is a long, long string """ Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 11 / 40
Concept: Variable A variable is a reference to an object. An object may have several references. One uses the assignment operator = to assign a value to a variable. Example x = [ 3, 4] # a list object is created y = x # this object now has two labels : x and y del x # we delete one of the labels del y # both labels are removed : the object is deleted Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 12 / 40
Concept: A python list is an ordered list of objects, enclosed in square brackets. One accesses elements of a list using zero-based indices inside square brackets. Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 13 / 40
List Examples Example L1 = [1, 2] L1[0] # 1 L1[1] # 2 L1[ 2] # raises IndexError L2 = [ a, 1, [3, 4]] L2[0] # a L2[2][0] # 3 L2[-1] # last element : [3,4] L2[-2] # second to last : 1 Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 14 / 40
List Utilities range(n) creates a list with n elements, starting with zero: print range (5) [0, 1, 2, 3, 4] Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 15 / 40
List Utilities range(n) creates a list with n elements, starting with zero: print range (5) [0, 1, 2, 3, 4] len(l) gives the length of a list: len ([ a, 1, 2, 34]) # returns 4 Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 15 / 40
List Utilities range(n) creates a list with n elements, starting with zero: print range (5) [0, 1, 2, 3, 4] len(l) gives the length of a list: len ([ a, 1, 2, 34]) # returns 4 Use append to append an element to a list: L = [ a, b, c ] L[-1] # c L. append ( d ) L # L is now [ a, b, c, d ] L[-1] # d Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 15 / 40
Comprehensive lists A convenient way to build up lists is to use the comprehensive lists construct, possibly with a conditional inside. Definition The syntax of a comprehensive list is [< expr > for <x> in < list >] Example L = [2, 3, 10, 1, 5] L2 = [x*2 for x in L] # [4, 6, 20, 2, 10] L3 = [x*2 for x in L if 4 < x <= 10] # [20, 10] Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 16 / 40
Comprehensive in Maths Mathematical Notation This is very close to the mathematical notation for sets. Compare: and L2 = [2*x for x in L] S 2 = {2x; x S} One big difference though is that lists are ordered while sets aren t. Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 17 / 40
Operations on Adding two lists concatenates (sammanfoga) them: L1 = [1, 2] L2 = [3, 4] L = L1 + L2 # [1, 2, 3, 4] Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 18 / 40
Operations on Adding two lists concatenates (sammanfoga) them: L1 = [1, 2] L2 = [3, 4] L = L1 + L2 # [1, 2, 3, 4] Logically, multiplying a list with an integer concatenates the list with itself several times: n*l is equivalent to L + L + + L. } {{ } n times L = [1, 2] 3 * L # [1, 2, 1, 2, 1, 2] Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 18 / 40
Concept: for loop for loop A for loop allows to loop through a list using an index variable. This variable is successively equal to all the elements in the list. Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 19 / 40
Concept: for loop for loop A for loop allows to loop through a list using an index variable. This variable is successively equal to all the elements in the list. Example L = [1, 2, 10] for s in L: print s * 2, # output : 2 4 20 Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 19 / 40
Indentation The part to be repeated in the for loop has to be properly indented: for elt in my_ list : do_something () something_ else () etc print " loop finished " # outside the for block Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 20 / 40
Repeating a Task One typical use of the for loop is to repeat a certain task a fixed number of time: n = 30 for i in range (n): do_ something # this gets executed n times Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 21 / 40
1 Python vs Other Languages Examples and Demo 2 3 Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 22 / 40
Python Shell Start a python session by typing scipython in a unix shell Check that it is working with: plot(rand(4));show() A window should appear with a graph; you should be able to type other commands without having to close the graph window when you want to quit, write exit() When you want to run python at home please follow the installation instruction on http://www.maths.lth.se/na/python/install Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 23 / 40
Executing Scripts You often want to execute the contents of a file. We recommand to use Kate on the Linux machines (but any other good editor will do) Save your files in (for example) in $HOME/course/ Type (once) in ipython: cd course To execute the contents of a file named file.py just write execfile( file.py ) in ipython. Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 24 / 40
Getting Help Some tips on how to use ipython: To get help on an object just type? after it and then return Use the arrow keys to reuse the last executed commands We will see later that you may use the tabulation key for completion in general Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 25 / 40