Documente Academic
Documente Profesional
Documente Cultură
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 1
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 2
Python
Invented in 1990 as teaching language Designed for clearness and readability Named after Monty Python (not the snake)
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 3
machines and clusters, web servers, Palm and other Handhelds, Nokia Mobiles, .NET virtual machines, . . .
Versatile Easy to learn, understand, use, and maintain (really!) Free, open-source ...
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 4
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 5
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 6
Note
Were assuming Python 2.5.X, 2.6.X, or 2.7.X The current version Python 3.X requires few changes
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 6
Requirements
independent of Python
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 7
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 8
$ python Python 2.7.3 ( default , Aug 1 2012 , 05:14:39) [ GCC 4.6.3] on linux2 Type " help " , " copyright " , " credits " or " license " for more information . >>> help Type help () for interactive help , or help ( object ) for help about object . >>> help ()
Welcome to Python 2.7! utility . [...] This is the online help
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 9
Getting help
Literature
David M. Beasley: Python - Essential Reference, Addison-Wesley Professional, 4th edition, 2009 Hans Petter Langtangen: A Primer on Scientic Programming with Python, Springer, 2009
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 10
assignment: variableName = expression (different to math. =) Interactive session shows return values print prints variables and expressions as string
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 12
1. Start the Python interpreter with python 2. Use IPython: ipython 3. Run Python on a le 3.5. Execute as a script
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 13
2. IPython
$ ipython Python 2.7.3 ( default , Aug 1 2012 , 05:14:39) Type " copyright " , " credits " or " license " for more information .
IPython 0.13.2. rc2 -- An enhanced Interactive Python . ? -> Introduction and overview of IPython s features . % quickref -> Quick reference . help -> Python s own help system . object ? -> Details about object . ? object also works , ?? prints more . In [1]: print " Hello World !" Hello World !
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 14
2. IPython
Highlights
Enhanced interactive shell Additional shell syntax
magic functions, starting with % such as %psearch to search for function in namespace Access shell commands %cd Run scripts with run filename.py Log session, dene macros, . . . Pretty printing, toggle with %Pprint
Automatic indentation
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 15
$ python square_me . py 36
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 16
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 17
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 18
General Remarks
Everything is an object (int, oat, tuple, classes, . . . ) Thus everything can have attributes and methods c = complex (2 ,3) print c . real , c . imag Dynamic typing: Type of a variable determined during Automatic conversion where necessary and possible a = 1234 type ( a ) b = 100 type ( b ) a = 1234**100 type ( a ) b = b *1.0 # equals b = float ( b )*1.0 type ( b )
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 21
assignment
Built-in Sequences
Strings
string type Sequence of characters Single, double or triple double (multiline) quotes " ... there lived a hobbit . " " a hobbit " a " hobbit " a \ hobbit \ """ In a hole in the ground there " lived " a hobbit """ Concatenation s1 = " In a hole in the ground " s2 = " there lived a hobbit " s = s1 + + s2 print s
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 22
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 23
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 24
Built-in Sequences
Tuples Sequences of arbitrary objects (,) Immutable Indexing and slicing as before
l = (1 ,) l = (1 , " two " , (3 ,4 ,5)) l [0] l [ -1] l [1] = 2 # error len ( l ) min ( l ) # here : numbers smaller than strings l = ( " Hello " , " Hobbit " ) min ( l ) max ( l ) l2 = l + (6 ,7 ,8)
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 25
Built-in Sequences
Lists Sequences of arbitrary objects [,] Mutable
l = [1 , " two " , (3 ,4 ,5) , 5] l [0] l [ -1] l [1] = 2 # works ! len ( l ) Operations on lists l = [0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9] l [2:4] = [10 , 11] l [1:7:2] = [ -1 , -2 , -3] del l [::2]
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 26
Built-in Sequences
Lists (2) List methods
l = [0 ,1 ,2 ,3] l . append ( " four " ) l . extend ([5 ,6 ,7 ,8 ,9]) l . insert (8 , " four " ) l . count ( " four " ) l . sort () l . reverse () l . remove ( " four " ) l . pop () Integer ranges: range function range (10) range (5 , 10) range ( -10 , 20 , 3)
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 27
Control Flow
General
Indentation used to group blocks! Code within same block has to be same level Needs getting used to, but encourages readable code
if-statement
if x < y : print print elif x == print else : print print " x is smaller " x y: " both are equal " " y is smaller " y
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 28
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 29
Combining and grouping (and, or, not) if ( " Bilbo " != " Frodo " and not (1 > 2 or 2 > 3)): print " Sam was here "
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 30
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 31
Functions (2)
Default arguments def printme ( s = " Frodo " ): print s printme () printme ( " Bilbo " ) Keyword arguments def exp ( basis , exponent ): return basis ** exponent exp ( basis =2 , exponent =10) exp ( exponent =10 , basis =2)
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 34
Up to now. . .
Variables Numeric types Sequence types (str, tuple, list; more tomorrow) if comparison for, while, break, continue Functions and parameters
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 35