Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PyConZA 2012: "Python for Programmers" by Umonya (Nina Schiff and Lyndsay Lawrence)

Pycon ZA
October 04, 2012

PyConZA 2012: "Python for Programmers" by Umonya (Nina Schiff and Lyndsay Lawrence)

A tutorial aimed at introducing Python to programmers. The tutorial will cover basic Python concepts, such as datastructures (and their manipulation), functions and classes. The talk will also touch on some more advanced topics, including lambda functions and functional-style programming, Regular Expressions and very basic networking. The tutorial section will be followed by a practical coding session, where participants will be asked to fill in the AI portion for a capture-the-flag, maze-style game.

A brief and speedy introduction to Python programming fundamentals as well as some of the stuff that makes Python a bit magic. The talk is aimed at individuals who already have some programming experience, although not necessarily in Python. The session will finish off with some hands-on exposure to the language through the form of a tournament style capture-the-flag game. Here participants will be required to code some basic movement AI, in order to pit their AI against those of other participants.

Pycon ZA

October 04, 2012
Tweet

More Decks by Pycon ZA

Other Decks in Programming

Transcript

  1.  Open source  C and Java (Jython) flavours 

    Highly extensible  Easy to write and integrate additional modules  Supports multiple programming paradigms  OO, structured, functional, aspect-oriented  Dynamically typed (and strongly typed)  Yeeeeah!  Automatic memory management  Yeeeeah!  “beautiful”, “explicit”, “simple”  Yeeeeah!  Named after Monty Python  Knights of Nih? Overview
  2.  Interactive shell!  (Yeeeeah!) Interpreter > > > 1+

    1 2 > > > pri nt ' hel l o w orl d' hel l o w orl d > > > x = 1 > > > y = 2 > > > x + y 3
  3.  Indentation (really) matters  Interpreted language  Everything is

    an object  All objects can be assigned to variable, passed to function  Some have attributes, methods  Some are subclassable  But everything is an object  Strings, Lists, Functions, Modules  EVERYTHING Some things to note…
  4. w hi l e condi t i on: # code

    f or i t em i n i t erabl e: # code i f i t em i n dat ast ruct ur e: # code el i f condi t i on1 and condi t i on2: # m ore code el se: # and m ore Some basic syntax
  5. def bui l dConnect i onSt ri ng( param s)

    : """Bui l d a connect i on st ri ng f r om a di ct i onary of param et ers. Ret ur ns st ri ng. """ r et ur n "; ". j oi n( [ "% s= % s" % ( k, v) f or k, v i n param s. i t em s( ) ] ) i f __nam e__ = = "__m ai n__": m yParam s = {"server": "m pi l gri m ", \ "dat abase": "m ast er", \ "ui d": "sa", \ "pw d": "secr et " \ } pri nt bui l dConnect i onSt ri ng( m yParam s) And bam!
  6. def bui l dConnect i onSt ri ng( param s)

    : """Bui l d a connect i on st ri ng f r om a di ct i onary of param et ers. Ret ur ns st ri ng. """ r et ur n "; ". j oi n( [ "% s= % s" % ( k, v) f or k, v i n param s. i t em s( ) ] ) i f __nam e__ = = "__m ai n__": m yParam s = {"server": "m pi l gri m ", \ "dat abase": "m ast er", \ "ui d": "sa", \ "pw d": "secr et " \ } pri nt bui l dConnect i onSt ri ng( m yParam s) Functions…
  7. def f unct i onNam e ( param 1 =

    “def aul t ”, param 2) : """ Som e descri pt i on of t hi s f unct i on """ # Your code goes here r et urn som eVal ue, andAnot her Functions… Function attributes accessible at runtime – introspection! • f unct i onNam e. __doc__ Return values: None. One. Many. • aVal ue, bVal ue = f unct i onNam e( param 1, param 2)
  8. import > > > hel p( ) > > >

    i m port m at h > > > m at h. f abs( - 0. 5) 0. 5 > > f r om m at h i m port f abs > > f abs( - 0. 5) 0. 5 > > > m at h. __doc__ ' Thi s m odul e i s al w ays avai l abl e. I t pr ovi des access t o t he m at hem at i cal f unct i ons def i ned by t he C st andar d. ' > > > m at h. __nam e__ ' m at h'
  9. def bui l dConnect i onSt ri ng( param s)

    : """Bui l d a connect i on st ri ng f r om a di ct i onary of param et ers. Ret ur ns st ri ng. """ r et ur n "; ". j oi n( [ "% s= % s" % ( k, v) f or k, v i n param s. i t em s( ) ] ) i f __nam e__ = = "__m ai n__": m yParam s = {"server": "m pi l gri m ", \ "dat abase": "m ast er", \ "ui d": "sa", \ "pw d": "secr et " \ } pri nt bui l dConnect i onSt ri ng( m yParam s) __main__
  10. def bui l dConnect i onSt ri ng( param s)

    : """Bui l d a connect i on st ri ng f r om a di ct i onary of param et ers. Ret ur ns st ri ng. """ r et ur n "; ". j oi n( [ "% s= % s" % ( k, v) f or k, v i n param s. i t em s( ) ] ) i f __nam e__ = = "__m ai n__": m yParam s = {"server": "m pi l gri m ", \ "dat abase": "m ast er", \ "ui d": "sa", \ "pw d": "secr et " \ } pri nt bui l dConnect i onSt ri ng( m yParam s) And data structures?
  11.  Dictionary di ct i onary = {"a": 2, 33:

    f unct i onNam e}  Li st l i st = [ "one", 2, obj ect Thr ee, {"f our" : 4}]  Tupl e t upl e = ( 1, "t w o", obj ect Thr ee) {…} […] (…)
  12. > > > a = {} > > > a[

    "f i rst "] = 1 > > > a[ 2] = "second" > > > a. keys( ) [ 2, ' f i rst ' ] > > > del a[ 2] > > > a {' f i rst ' : 1} {…}
  13.  Immutable > > > a = ( 1, "t

    w o", 3) > > > a[ 0] 1 > > > 3 i n a True (…)
  14. > > > a [ ' a' , ' b'

    , ' c' ] > > > a. append( [ "e", "f "] ) [ ' a' , ' b' , ' c' , ' d' , [ ' e' , ' f ' ] ] > > > a. ext end( [ "h", "i "] ) [ ' a' , ' b' , ' c' , ' d' , [ ' e' , ' f ' ] , ' h' , ' i ' ] > > > a. i nsert ( 5, "g”) [ ' a' , ' b' , ' c' , ' d' , [ ' e' , ' f ' ] , ' g' , ' h' , ' i ' ] […]
  15. > > > a[ 0] ' f i rst '

    > > > a[ - 1] ' f i f t h' > > > a[ 2: 4][ ' t hi r d' , ' f ourt h' ] > > > a[ : 3] [ ' f i rst ' , ' second' , ' t hi r d' ] > > > a[ - 3: ][ ' t hi r d' , ' f ourt h' , ' f i f t h' ] > > > a. r em ove( "second”) > > > a. pop( ) ' f i f t h’ […]
  16. > > > a = [ 1, 2, 3] >

    > > a + = [ 4, 5] [ 1, 2, 3, 4, 5] > > > a = [ 1, 2] > > > a = a*3 [ 1, 2, 1, 2, 1, 2] […]
  17. > > > a = "123" > > > a

    + = "45” ' 12345’ > > > a = "12" > > > a = a*3 ' 121212’ > > > "% d, % s and % s" % ( 5, "hey! ", [ 1, ' 2' , 3] ) "5, hey! and [ 1, ' 2' , 3] " “…”
  18. new Li st = [ ] f or i i

    n [ "a", "b", "c"] : new Li st . append( i *2) Loops, lists and mappings new Li st = [i *2 for i i n ["a", "b", "c"]]
  19. new Li st = [ ] f or i i

    n range( 5) : i f i % 2 = = 0: new Li st . append( i ) Loops, lists and filtering new Li st = [ i f or i i n range( 5) i f i % 2= = 0 ]
  20. def bui l dConnect i onSt ri ng( param s)

    : """Bui l d a connect i on st ri ng f r om a di ct i onary of param et ers. Ret ur ns st ri ng. """ r et ur n "; ". j oi n( [ "% s= % s" % ( k, v) f or k, v i n param s. i t em s( ) ] ) i f __nam e__ = = "__m ai n__": m yParam s = {"server": "m pi l gri m ", \ "dat abase": "m ast er", \ "ui d": "sa", \ "pw d": "secr et " \ } pri nt bui l dConnect i onSt ri ng( m yParam s) ?!
  21. > > > param s {' pw d' : '

    secr et ' , ' dat abase' : ' m ast er' , ' ui d' : ' sa' , ' server' : ' m pi l gri m ' } > > > param s. i t em s( ) [ ( ' pw d' , ' secr et ' ) , ( ' dat abase' , ' m ast er' ) , ( ' ui d' , ' sa' ) , ( ' server' , ' m pi l gri m ' ) ] > > > [ ( k, v) f or k, v i n param s. i t em s( ) ] [ ( ' pw d' , ' secr et ' ) , ( ' dat abase' , ' m ast er' ) , ( ' ui d' , ' sa' ) , ( ' server' , ' m pi l gri m ' ) ] > > > [ "% s= % s" % ( k, v) f or k, v i n param s. i t em s( ) ] [ ' pw d= secr et ' , ' dat abase= m ast er' , ' ui d= sa' , ' server= m pi l gri m ' ] > > > "; ". j oi n( [ ' pw d= secr et ' , ' dat abase= m ast er' , ' ui d= sa' , ' server= m pi l gri m ' ] ) > > > "; ". j oi n( [ "% s= % s" % ( k, v) f or k, v i n param s. i t em s( ) ] ) ' pw d= secr et ; dat abase= m ast er; ui d= sa; server= m pi l gri m ' Iterate + format + join
  22. def bui l dConnect i onSt ri ng( param s)

    : """Bui l d a connect i on st ri ng f r om a di ct i onary of param et ers. Ret ur ns st ri ng. """ r et ur n "; ". j oi n( [ "% s= % s" % ( k, v) f or k, v i n param s. i t em s( ) ] ) i f __nam e__ = = "__m ai n__": m yParam s = {"server": "m pi l gri m ", \ "dat abase": "m ast er", \ "ui d": "sa", \ "pw d": "secr et " \ } pri nt bui l dConnect i onSt ri ng( m yParam s) And bam!
  23. cl ass Par ent Cl ass: """ Par ent cl

    ass "”” al i st = [ ] def __i ni t __( sel f , l i st Val ues = [ ] ) : sel f . __cl ass__. al i st = l i st Val ues cl ass Cl assNam e( Par ent Cl ass) : """ Chi l d cl ass t hat adds t o l i st """ def __i ni t __( sel f , nam e= None) : # Ki nd of l i ke a const ruct or . But not . Par ent Cl ass. __i ni t __( sel f , [ "f i rst "] ) sel f . nam e = nam e def addVal ue( sel f , val ue) : ""” Adds a val ue t o l i st "”” sel f . al i st . append( val ue) r et ur n "Added: ", val ue i f __nam e__ = = "__m ai n__": a = Cl assNam e( "Nam e") pri nt a. addVal ue( "b") pri nt a. nam e
  24. cl ass Par ent Cl ass: """ Par ent cl

    ass "”” al i st = [ ] def __i ni t __( sel f , l i st Val ues = [ ] ) : sel f . __cl ass__. al i st = l i st Val ues cl ass Cl assNam e( Par ent Cl ass) : """ Chi l d cl ass t hat adds t o l i st """ def __i ni t __( sel f , nam e= None) : # Ki nd of l i ke a const ructor . But not . Par ent Cl ass. __i ni t __( sel f , [ "f i rst "] ) sel f . nam e = nam e def addVal ue( sel f , val ue) : ""” Adds a val ue t o l i st "”” sel f . al i st . append( val ue) r et ur n "Added: ", val ue i f __nam e__ = = "__m ai n__": a = Cl assNam e( "Nam e") pri nt a. addVal ue( "b") pri nt a. nam e
  25. cl ass Par ent Cl ass: """ Par ent cl

    ass "”” al i st = [ ] def __i ni t __( sel f , l i st Val ues = [ ] ) : sel f . __cl ass__. al i st = l i st Val ues cl ass Cl assNam e( Par ent Cl ass) : """ Chi l d cl ass t hat adds t o l i st """ def __i ni t __( sel f , nam e= None) : # Ki nd of l i ke a const ruct or . But not. Par ent Cl ass. __i ni t __(sel f , [ "f i rst "] ) sel f . nam e = nam e def addVal ue( sel f , val ue) : ""” Adds a val ue t o l i st "”” sel f . al i st . append( val ue) r et ur n "Added: ", val ue i f __nam e__ = = "__m ai n__": a = Cl assN am e( "N am e") pri nt a. addVal ue( "b") pri nt a. nam e
  26. cl ass Par ent Cl ass: """ Par ent cl

    ass "”” al i st = [ ] def __i ni t __( sel f , l i st Val ues = [ ] ) : sel f . __cl ass__. al i st = l i st Val ues cl ass Cl assNam e( Par ent Cl ass) : """ Chi l d cl ass t hat adds t o l i st """ def __i ni t __( sel f , nam e= None) : # Ki nd of l i ke a constructor . But not. Par ent Cl ass. __i ni t __( sel f , [ "f i rst "] ) sel f . nam e = nam e def addVal ue( sel f , val ue) : ""” Adds a val ue t o l i st "”” sel f . __cl ass__. al i st . append( val ue) r et ur n "Added: ", val ue i f __nam e__ = = "__m ai n__": a = Cl assN am e( "N am e") pri nt a. addVal ue( "b") pri nt a. nam e
  27.  Multiple inheritance  cl ass A ( B, C)

    :  sel f keyword required as first argument of every method  def f unct i on( sel f , ar g1 = "def aul t val ue"):  __i ni t __ is kind of like a constructor. But not.  Private objects start with __  def __pri vat eFunct i on( sel f ) :  Data attributes declared in __i ni t __ and referenced using self.  Class attributes declared in body of class, and referenced using sel f . __cl ass__ Classes
  28.  “a lambda function is a function that takes any

    number of arguments (including optional arguments) and returns the value of a single expression” > > > g = l am bda x: x*2 > > > g( 3) 6 lambda
  29.  Supported natively  Methods to search, replace and parse

    pat t er n = ' ^ M {0, 4}( CM | CD | D ?C{0, 3}) ( XC| XL| L?X{0, 3}) ( I X| I V| V?I {0, 3}) $' Regex
  30.  Less about how and more about what you want

    to do. > > > f i l t er( l am bda x: x% 2, [ 1, 2, 3, 4, 5, 6] ) [ 1, 3, 5] > > > m ap(l am bda x: x*2, [ 1, 2, 3, 4, 5, 6] ) [ 2, 4, 6, 8, 10, 12] Datacentric programming
  31.  “resumable functions that perform incremental logic and return different

    values each time you call them”  Lazy evaluation  Can make use of closures to create dynamic functions f or l i ne i n f i l e( "som eFi l e") : pri nt l i ne Generators
  32. def f i bon( n) : a = b =

    1 f or i i n xrange( n) : yi el d a a, b = b, a + b f or x i n f i bon( 1000000) : pri nt x, Example