Python Tutorial Michael Muenzer Graz University of Technology - - PowerPoint PPT Presentation

python
SMART_READER_LITE
LIVE PREVIEW

Python Tutorial Michael Muenzer Graz University of Technology - - PowerPoint PPT Presentation

Python Tutorial Michael Muenzer Graz University of Technology March 18, 2013 Slides based on previous work by Jan P oschko and Klaus Potzmader Introduction Language basics Advanced concepts Special modules Why Python? Python is:


slide-1
SLIDE 1

Python

Tutorial Michael Muenzer

Graz University of Technology

March 18, 2013

Slides based on previous work by Jan P¨

  • schko and Klaus Potzmader
slide-2
SLIDE 2

Introduction Language basics Advanced concepts Special modules

Why Python?

Python is:

  • very readable
  • easy to learn
  • interpreted & interactive – like a UNIX shell, only better
  • object-oriented – but not religious about it
  • slower than C, but it is easy to integrate C, Fortran, Java

“Batteries included”

Python Michael Muenzer

slide-3
SLIDE 3

Introduction Language basics Advanced concepts Special modules

The Zen of Python

  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • Simple is better than complex.
  • Complex is better than complicated.
  • Flat is better than nested.
  • Sparse is better than dense.
  • Readability counts.
  • Special cases aren’t special enough to break the rules.
  • Although practicality beats purity.
  • Errors should never pass silently.
  • Unless explicitly silenced.
  • In the face of ambiguity, refuse the temptation to guess.
  • There should be one – and preferably only one – obvious way to do it.
  • Although that way may not be obvious at first unless you’re Dutch.
  • Now is better than never.
  • Although never is often better than right now.
  • If the implementation is hard to explain, it’s a bad idea.
  • If the implementation is easy to explain, it may be a good idea.
  • Namespaces are one honking great idea – let’s do more of those!

Python Michael Muenzer

slide-4
SLIDE 4

Introduction Language basics Advanced concepts Special modules

Installing Python

  • Included in most distributions of Linux and Mac OS X
  • Downloadable from http://www.python.org/download/
  • Libraries:
  • NumPy: http://sourceforge.net/projects/numpy/
  • matplotlib: http://sourceforge.net/projects/

matplotlib/files/matplotlib/

  • NetworkX: http://networkx.lanl.gov/install.html
  • Editors:
  • Eclipse
  • emacs, vim
  • . . . (anything that knows how to handle tabs and spaces)

Python Michael Muenzer

slide-5
SLIDE 5

Introduction Language basics Advanced concepts Special modules

Invoking Python

  • Interactive mode: Open a console/terminal window and run

python (fancier alternative: ipython)

  • Execution: python myfile.py or execfile(’myfile.py’)
  • Make sure you have python 2.7.3 installed as it’s the version

used in our tests

Python Michael Muenzer

slide-6
SLIDE 6

Introduction Language basics Advanced concepts Special modules

Language basics

n = 42 # i n t e g e r x = 3.14159 # f l o a t x = ” Hello world ! ” # s t r i n g

  • No variable declarations
  • Dynamically typed

String delimeters: s = ” Hello world ! ” s = ’ Hello world ! ’ s = ””” Hello world ! ””” # multi −l i n e s t r i n g

Python Michael Muenzer

slide-7
SLIDE 7

Introduction Language basics Advanced concepts Special modules

Booleans

  • True is true, False is false
  • 0, "" and None are false, (almost) everything else is true

not A A and B A or B Comparisons: ==, !=, <, <=, >, >= 2 == 2 1 < 2 <= 3 1 == 2 and ”3” or ”4” # = ”4”

Python Michael Muenzer

slide-8
SLIDE 8

Introduction Language basics Advanced concepts Special modules

Lists

L = [1 , 2 , 3] L [ 0 ] # = 1 L [ 1 : 3 ] # = [2 , 3] L [: −1] # = [1 , 2] L . append (4) # −> [1 , 2 , 3 , 4] L += [5 , 6] # −> [1 , 2 , 3 , 4 , 5 , 6] del L [ 5 ] # −> [1 , 2 , 3 , 4 , 5] l e n (L) # = 5 L . r e v e r s e () # −> [5 , 4 , 3 , 2 , 1] L . s o r t () # −> [1 , 2 , 3 , 4 , 5] Variables only hold references to lists (like to everything else): M = L M[ 2 ] = ”A” # −> [1 , 2 , ”A” , 4 , 5] L [ 2 ] # −> ”A”

Python Michael Muenzer

slide-9
SLIDE 9

Introduction Language basics Advanced concepts Special modules

Dictionaries and sets

Dictionaries: D = {”Mozart” : 1756 , ” Schubert ” : 1797} D[ ”Mozart” ] # = 1756

  • D. keys ()

# = [ ’ Schubert ’ , ’ Mozart ’ ]

  • D. v a l u e s ()

# = [1797 , 1756]

  • D. update ({ ” Beethoven ” :

1770}) D[ ” E i n s t e i n ” ] = 1879 ” E i n s t e i n ” in D # = True l e n (D) # = 4

  • D. get ( ”Newton” , ”unknown” )

# = ’ unknown ’ Sets: s = s e t ( [ 1 , ” h e l l o ” , ” world ” ] ) s . add (3) 2 in s # = False

Python Michael Muenzer

slide-10
SLIDE 10

Introduction Language basics Advanced concepts Special modules

Output and string formatting

Basic printing print ”two : ” , 2 , ” four : ” , 4 # two : 2 four : 4 print ”%0.2 f + %d = %0.2 f ” %(2.5 , 2 , 2.5+2) # 2.50 + 2 = 4.50 str.format() formatted = ”PI : {0:0 .2 f }” . format ( math . pi ) print formatted # PI : 3.14 Newline peculiarities: print ” t e x t ” # appends <newline > print ” t e x t ” , # appends a space ( note the comma) import sys sys . stdout . w r i t e ( ” t e x t ” ) # appends nothing

http://docs.python.org/library/string.html#formatstrings

Python Michael Muenzer

slide-11
SLIDE 11

Introduction Language basics Advanced concepts Special modules

Control flow

i f n == 0: print ”n i s 0” e l i f n > 0: print ”n i s p o s i t i v e ” else : print ”n i s n e g a t i v e ”

  • Indentation matters!
  • Don’t mix tabs and spaces – configure your editor

appropriately! while n > 0: n −= 1 for n in [1 , 2 , 3 , 4 ] : print n

Python Michael Muenzer

slide-12
SLIDE 12

Introduction Language basics Advanced concepts Special modules

Idioms

Iterate over a dictionary and format strings: D = {”Mozart” : 1756 , ” Schubert ” : 1797} for name , year in D. i t e r i t e m s ( ) : print ”%s was born in %d” % (name , year ) Enumerate list: L = [ ” item1 ” , ” item2 ” , ” item3 ” , ” item4 ” ] for i , item in enumerate (L ) : print ”The l i s t c o n t a i n s %s at index %d” \ % ( item , i ) List comprehensions: quad = [ x∗∗2 for x in range ( 8 ) ] # = [0 , 1 , 4 , 9 , 16 , 25 , 36 , 49] even = [ x∗∗2 for x in range (8) i f x % 2 == 0] # = [0 , 4 , 16 , 36]

Python Michael Muenzer

slide-13
SLIDE 13

Introduction Language basics Advanced concepts Special modules

Files

v a l u e s = [ [ 1 , 0] , [1 , 1 ] ] # Join v a l u e s by ” ,” and l i n e s by newlines c s v t e x t = ’ \n ’ . j o i n ( ’ , ’ . j o i n ( s t r ( value ) \ for value in l i n e ) for l i n e in v a l u e s ) # Write the t e x t i n t o a f i l e c s v f i l e = open ( ’ f i l e . csv ’ , ’w ’ ) c s v f i l e . w r i t e ( c s v t e x t ) c s v f i l e . c l o s e () with

  • pen ( ” m y f i l e . t x t ” )

as f : data = f . read () # do something # f i l e i s c l o s e d upon l e a v i n g the ’ with ’ block

Python Michael Muenzer

slide-14
SLIDE 14

Introduction Language basics Advanced concepts Special modules

Functions: fac(n) = n! := n

k=1 k

def fac (n ) : i f n == 1: return 1 else : return n ∗ fac (n − 1) def fac (n ) : r e s u l t = 1 for k in range (1 , n + 1 ) : r e s u l t ∗= k return r e s u l t def fac (n ) : return reduce (lambda a , b : a ∗ b , range (1 , n + 1) , 1)

Python Michael Muenzer

slide-15
SLIDE 15

Introduction Language basics Advanced concepts Special modules

Functions: Parameters

Parameters point to values (like all variables): def change (a , b ) : a = b [ 0 ] = 0 a , b = 1 , [1 , 1] change (a , b) # −> a = 1 , b = [0 , 1] Named Parameters: def h e l l o w o r l d ( h e l l o=” Hello ” , world=” world ” ) : print h e l l o , world h e l l o w o r l d () # −> ” Hello world ” h e l l o w o r l d ( h e l l o=”Hi” ) # −> ”Hi world ” # p o s i t i o n does not matter : h e l l o w o r l d ( world=” earth ” , h e l l o=”Hi” ) # −> ”Hi earth ”

Python Michael Muenzer

slide-16
SLIDE 16

Introduction Language basics Advanced concepts Special modules

Functions: Parameters 2

Special list and keyword (dictionary) arguments

  • starred for list, double-starred for dictionary
  • Enable arbitrary number of arguments
  • Restriction if used in combination: keyword arguments need

to be after the list arguments def fun (∗ args , ∗∗ kwargs ) : # do something with v a l u e s fun ( ”some” , ” l i s t ” , ” v a l u e s ” , with=” key ” , value=” p a i r s ” , as=” d i c t i o n a r y ” ) E.g.: def sum(∗ v a l u e s ) : # sum (1 ,2 ,5) = 8 r e s u l t = 0 for value in v a l u e s : r e s u l t += value return r e s u l t

Python Michael Muenzer

slide-17
SLIDE 17

Introduction Language basics Advanced concepts Special modules

Iterables

  • Basically everything you can use in for .. in ..
  • e.g.

m y l i s t = [ x∗∗2 for x in range ( 2 , 5 ) ] for i in m y l i s t : print i mystr = ” h e l l o w o r l d ” for char in mystr : print char

  • Can be read multiple times
  • Downside: Everything is in memory as a whole

Python Michael Muenzer

slide-18
SLIDE 18

Introduction Language basics Advanced concepts Special modules

Generators

  • Special iterables that generate values on the fly
  • Not in memory as a whole
  • Thus, only readable once

Same example as before, with generators: # note the d i f f e r e n t braces mygen = ( x∗∗2 for x in range (2 ,5)) print mygen # −> <generator

  • b j e c t

... > for i in mygen : print i # a second loop won ’ t p r i n t anything as the # generator a l r e a d y went through a l l items

  • as soon as the loop wants to read a new value, it is

’generated’ from the comprehension rule given above

Python Michael Muenzer

slide-19
SLIDE 19

Introduction Language basics Advanced concepts Special modules

Generators 2

def r e v e r s e ( data ) : for index in range ( l e n ( data )−1, −1, −1): y i e l d data [ index ] for char in r e v e r s e ( ’ webscience ’ ) : print char

  • Yield temporarily suspends processing and remembers the

execution state

  • Upon resume, the generator picks up where it left off
  • More at
  • docs.python.org/tutorial/classes.html#generators
  • stackoverflow.com/a/231855

Python Michael Muenzer

slide-20
SLIDE 20

Introduction Language basics Advanced concepts Special modules

Generators 3

Read file line by line and split by ’,’: with

  • pen ( ’ t e x t f i l e . t x t ’ ,

’ r ’ ) as f : l i n e s = ( l i n e . r s t r i p ( ’ \n ’ ) . s p l i t ( ’ , ’ ) for l i n e in f ) for l i n e in l i n e s : print l i n e # process here E.g. if testfile.txt contains

  • ne,two,three

3,2,1 we get [’one’, ’two’, ’three’] [’3’, ’2’, ’1’]

Python Michael Muenzer

slide-21
SLIDE 21

Introduction Language basics Advanced concepts Special modules

Classes and objects

class Animal : def i n i t ( s e l f , name ) : s e l f . name = name def s a y h e l l o ( s e l f ) : print ” I ’m %s ” % s e l f . name class Dog( Animal ) : def i n i t ( s e l f , name ,

  • wner ) :

super (Dog , s e l f ) . i n i t (name) s e l f . owner = owner def s a y h e l l o ( s e l f ) : print ” Hello %s ” % s e l f . owner dog = Dog( ” Charly ” , ”Mike” ) dog . s a y h e l l o ()

Python Michael Muenzer

slide-22
SLIDE 22

Introduction Language basics Advanced concepts Special modules

Modules

Any Python file (e.g. mymodule.py) is a module and can be imported: import mymodule mymodule . myfunction () from mymodule import myfunction from mymodule import ∗ # r a t h e r discouraged myfunction () File-system directories can be used to create “namespaces”, if they contain a file init .py (which may contain initialization code): import mydir . mymodule from mydir . mymodule import myfunction

Python Michael Muenzer

slide-23
SLIDE 23

Introduction Language basics Advanced concepts Special modules

main

Variable name contains the name of the current module. It equals " main " when the script is run directly from the command-line. Common idiom: def main ( ) : print ” Hello world ! ” i f name == ” main ” : main ()

Python Michael Muenzer

slide-24
SLIDE 24

Introduction Language basics Advanced concepts Special modules

More advanced concepts

  • exceptions
  • multiple inheritance
  • operator overloading
  • metaclasses
  • inline documentation and test code
  • extensive “runtime” information

Python Michael Muenzer

slide-25
SLIDE 25

Introduction Language basics Advanced concepts Special modules

Regular expressions

re is a module for handling regular expressions. import re re . f i n d a l l ( ’ [A−Za−z]+ ’ , ’ Hello world ! ’ ) # = [ ’ Hello ’ , ’ world ’ ] * 0 or more findall(pattern, string) + 1 or more match(pattern, string) ? 0 or 1 sub(pattern, repl, string) [. . . ] certain characters split(pattern, string) [^. . . ] characters excluded . . . Pre-compiling regular expressions: WORD RE = re . compile ( ’ [A−Za−z]+ ’ ) WORD RE. f i n d a l l ( ’ Hello world ! ’ )

Python Michael Muenzer

slide-26
SLIDE 26

Introduction Language basics Advanced concepts Special modules

URLs

urllib2 is a module for opening URLs. from u r l l i b 2 import urlopen u r l f i l e = urlopen ( ” http ://www. google . com” ) content = u r l f i l e . read () # = ’<! doctype html> <html >[...] </ s c r i p t >’ Basic HTTP authentication: import u r l l i b 2 auth handler = u r l l i b 2 . HTTPBasicAuthHandler () auth handler . add password ( realm=’ the realm ’ , u r i=’ http ://www. example . com ’ , user=’ usr ’ , passwd=’pwd ’ )

  • pener = u r l l i b 2 . b u i l d o p e n e r ( auth handler )

u r l l i b 2 . i n s t a l l o p e n e r ( opener ) urlopen ( ’ http ://www. example . com/ r e s t r i c t e d ’ )

Python Michael Muenzer

slide-27
SLIDE 27

Introduction Language basics Advanced concepts Special modules

XML documents

xml.dom is a module for parsing XML documents and accessing them using the Document Object Model. from xml . dom . minidom import p a r s e S t r i n g content = ”””<tag a t t r =”1”> <sub>text </sub></tag>””” dom = p a r s e S t r i n g ( content ) dom . childNodes [ 0 ] . g e t A t t r i b u t e ( ’ a t t r ’ ) # = ’1 ’ subs = dom . getElementsByTagName ( ’ sub ’ ) subs [ 0 ] . childNodes [ 0 ] . nodeValue # = ’ t e x t ’

Python Michael Muenzer

slide-28
SLIDE 28

Introduction Language basics Advanced concepts Special modules

JSON data

json is a module for parsing and exporting JSON data snippets. import json j s o n d a t a = ’ {” l i s t ”: [1 , 2] , ” key ”: ” value ”} ’ contents = json . loads ( j s o n d a t a ) print contents [ ’ l i s t ’ ] # −> [ 1 , 2 ] print contents [ ’ key ’ ] # −> value

Python Michael Muenzer

slide-29
SLIDE 29

Introduction Language basics Advanced concepts Special modules

NetworkX

networkx is a module for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. [networkx.lanl.gov] import networkx as nx G = nx . Graph ()

  • G. add node (1)
  • G. add edge (1 ,

2)

  • G. add edge (2 ,

3) Plot graphs: import m a t p l o t l i b . pyplot as p l t p l t . f i g u r e () nx . draw (G) p l t . s a v e f i g ( ’ graph . png ’ )

Python Michael Muenzer

slide-30
SLIDE 30

Introduction Language basics Advanced concepts Special modules

NetworkX: Customized visualization

p l t . f i g u r e () p l t . a x i s ( ’ o f f ’ ) pos = nx . s p r i n g l a y o u t (G, i t e r a t i o n s =50) nx . draw networkx edges (G, pos ) nx . draw networkx nodes (G, pos , [ 1 ] , node color=’ r ’ ) nx . draw networkx nodes (G, pos , [2 , 3] , node color=’b ’ ) nx . d r a w n e t w o r k x l a b e l s (G, pos ) p l t . s a v e f i g ( ’ graph . png ’ , dpi =72)

Python Michael Muenzer

slide-31
SLIDE 31

Further reading

Official Python tutorial: http://docs.python.org/tutorial/ Python tutorial at the University of Toronto: http: //www.cs.toronto.edu/~gpenn/csc401/401_python_web/ Python 2.7 Quick Reference: http://rgruet.free.fr/PQR27/PQR2.7.html http://docs.python.org/library/re.html http://docs.python.org/library/urllib2.html http://docs.python.org/library/xml.dom.html http://docs.python.org/library/json.html NetworkX: http://networkx.github.com/documentation/latest/

Python Michael Muenzer