Python
 Scott G. Ainsworth
      for ODU ACM
       22 Sept 2011
What is Python?

 Easy to learn

 Simple syntax

 Dynamic typing

 High-level data structures    Free

                                Linux/Unix, OS X, Windows

                                MIT’s language of choice
Why Python?
 Fast development cycle
   Interactive & edit, debug/run
   vs. edit, compile, debug/run

 Extensive standard and add-on libraries
   (MIT’s language of choice)

 Python, Jython, & IronPython

 Google AppEngine
Basic Syntax

 Block structured

 But no braces

 Indent defines blocks



                  def fib(n):
                      a, b = 0, 1
                      while b < n:
                          print b,
                          a, b = b, a+b
Numbers & Strings

 Integers: -231 – 231-1    'value' == "value"
    5 + 23
                            """multiple
 Long: unlimited            lines"""
    1234567890123456789    u'unicode'
 Boolean:                  'substring'[2:5] == 'bst'
    True or False
      0 or 1

 Real and Complex:
    1.25 / 6.3
    Complex(1.1,2.2)
Sequence Types

 Lists
    primes = [ 2, 3, 5, 7, 11, 13, 17, 19, 23 ]
    primes.append(27)
    Primes[-2:] = [ 23, 27 ]

 Tuples
    primes = ( 2, 3, 5, 7, 11, 13, 17,19, 23 )
    primes.append(27)  # fails, tuples are immutable
    primes[-2:] = ( 23, 27 )

 Strings are an immutable list of single characters
Dictionaries & Sets

 Dictionaries
   pdict = { "p1" : 2, "p2" : 3, "p3" : 5 }
   pdict["p4"] = 7
   pdict["p2"] == 3

 Sets
   primes = Set([ 2, 3, 5, 7, 11, 13, 17,19, 23 ])
   primes.append(27) # fails, set are unordered
   primes.add(27) # succeeds
Classes

Class complex:
    """implement complex numbers"""

   def __init__(self, r, i):
       self.__r = r
       self.__i = i

   def isreal(self):
       return self.c == 0

   def __add__(self, c):
       return complex(self.r + c.r, self.i + c.i)
Memento & Timemap for
    www.cs.odu.edu

http://api.wayback.archive.org/memento/timemap/link/http://www.cs.odu.edu
   1.   <http://www.cs.odu.edu>; rel="original",
   2.   <http://api.wayback.archive.org/list/timemap/link/http://www.cs.odu.edu>;
        rel="timemap"; type="application/link-format”,
   3.   <http://api.wayback.archive.org/memento/19970102130137/http://cs.odu.edu/>;
        rel="first memento"; datetime="Thu, 02 Jan 1997 13:01:37 GMT”,
   4.   <http://api.wayback.archive.org/memento/19970606105039/http://www.cs.odu.edu/
        >; rel="memento"; datetime="Fri, 06 Jun 1997 10:50:39 GMT",

Output from timemap.py test
   1.   Original: http://www.cs.odu.edu
   2.   Time Map:
        http://api.wayback.archive.org/list/timemap/link/http://www.cs.odu.edu
   3.   First Memento:
        (datetime.datetime(1997, 1, 2, 13, 1, 37, tzinfo=tzutc()), 'http://api.wayback.archive.o
        rg/memento/19970102130137/http://cs.odu.edu/')
   4.   1997-06-06 10:50:39+00:00 =
        http://api.wayback.archive.org/memento/19970606105039/http://www.cs.odu.edu/
timemap.py Output
Original:       http://www.cs.odu.edu
Time Bundle:    http://api.wayback.archive.org/list/timebundle/
                         http://www.cs.odu.edu
Time Gate:      http://api.wayback.archive.org/list/timegate/
                         http://www.cs.odu.edu
Time Map:       http://api.wayback.archive.org/list/timemap/link/
                         http://www.cs.odu.edu
First Memento: 1997-01-02 13:01:37+00:00
Last Memento: 2011-07-20 01:58:31+00:00
Mementos:
        1997-06-06 10:50:39+00:00 = http://api.wayback.archive.org/memento/
                19970606105039/http://www.cs.odu.edu/
        1997-10-10 20:16:32+00:00 = http://api.wayback.archive.org/memento/
                19971010201632/http://www.cs.odu.edu/
Code Walk Through
 class TimeMap: Memento timemap container class

 class TimeMapTokenizer: Helper class to tokenize a link-style
   timemap
 __main__: Used for quick unit testing




Download the code:
   http://www.cs.odu.edu/~sainswor/uploads/Downloads/timemap.py
Summary

 Easy to learn                 Fast development cycle

 Dynamic typing                Extensive standard and
                                 add-on libraries
 High-level data structures
                                Widely-supported
 Free

 Linux/Unix, OS X, Windows
Questions?
Links
 Slides: http://www.cs.odu.edu/~sainswor/uploads/Downloads/ACM-Python.py

 Code: http://www.cs.odu.edu/~sainswor/uploads/Downloads/timemap.py

 Python: http://www.python.org

 Python Docs: http://docs.python.org

 Python Libraries: http://pypi.python.org

ODU ACM Python & Memento Presentation

  • 1.
    Python Scott G.Ainsworth for ODU ACM 22 Sept 2011
  • 2.
    What is Python? Easy to learn  Simple syntax  Dynamic typing  High-level data structures  Free  Linux/Unix, OS X, Windows  MIT’s language of choice
  • 3.
    Why Python?  Fastdevelopment cycle  Interactive & edit, debug/run  vs. edit, compile, debug/run  Extensive standard and add-on libraries  (MIT’s language of choice)  Python, Jython, & IronPython  Google AppEngine
  • 4.
    Basic Syntax  Blockstructured  But no braces  Indent defines blocks def fib(n): a, b = 0, 1 while b < n: print b, a, b = b, a+b
  • 5.
    Numbers & Strings Integers: -231 – 231-1  'value' == "value"  5 + 23  """multiple  Long: unlimited lines"""  1234567890123456789  u'unicode'  Boolean:  'substring'[2:5] == 'bst'  True or False 0 or 1  Real and Complex:  1.25 / 6.3  Complex(1.1,2.2)
  • 6.
    Sequence Types  Lists  primes = [ 2, 3, 5, 7, 11, 13, 17, 19, 23 ]  primes.append(27)  Primes[-2:] = [ 23, 27 ]  Tuples  primes = ( 2, 3, 5, 7, 11, 13, 17,19, 23 )  primes.append(27) # fails, tuples are immutable  primes[-2:] = ( 23, 27 )  Strings are an immutable list of single characters
  • 7.
    Dictionaries & Sets Dictionaries  pdict = { "p1" : 2, "p2" : 3, "p3" : 5 }  pdict["p4"] = 7  pdict["p2"] == 3  Sets  primes = Set([ 2, 3, 5, 7, 11, 13, 17,19, 23 ])  primes.append(27) # fails, set are unordered  primes.add(27) # succeeds
  • 8.
    Classes Class complex: """implement complex numbers""" def __init__(self, r, i): self.__r = r self.__i = i def isreal(self): return self.c == 0 def __add__(self, c): return complex(self.r + c.r, self.i + c.i)
  • 9.
    Memento & Timemapfor www.cs.odu.edu http://api.wayback.archive.org/memento/timemap/link/http://www.cs.odu.edu 1. <http://www.cs.odu.edu>; rel="original", 2. <http://api.wayback.archive.org/list/timemap/link/http://www.cs.odu.edu>; rel="timemap"; type="application/link-format”, 3. <http://api.wayback.archive.org/memento/19970102130137/http://cs.odu.edu/>; rel="first memento"; datetime="Thu, 02 Jan 1997 13:01:37 GMT”, 4. <http://api.wayback.archive.org/memento/19970606105039/http://www.cs.odu.edu/ >; rel="memento"; datetime="Fri, 06 Jun 1997 10:50:39 GMT", Output from timemap.py test 1. Original: http://www.cs.odu.edu 2. Time Map: http://api.wayback.archive.org/list/timemap/link/http://www.cs.odu.edu 3. First Memento: (datetime.datetime(1997, 1, 2, 13, 1, 37, tzinfo=tzutc()), 'http://api.wayback.archive.o rg/memento/19970102130137/http://cs.odu.edu/') 4. 1997-06-06 10:50:39+00:00 = http://api.wayback.archive.org/memento/19970606105039/http://www.cs.odu.edu/
  • 10.
    timemap.py Output Original: http://www.cs.odu.edu Time Bundle: http://api.wayback.archive.org/list/timebundle/ http://www.cs.odu.edu Time Gate: http://api.wayback.archive.org/list/timegate/ http://www.cs.odu.edu Time Map: http://api.wayback.archive.org/list/timemap/link/ http://www.cs.odu.edu First Memento: 1997-01-02 13:01:37+00:00 Last Memento: 2011-07-20 01:58:31+00:00 Mementos: 1997-06-06 10:50:39+00:00 = http://api.wayback.archive.org/memento/ 19970606105039/http://www.cs.odu.edu/ 1997-10-10 20:16:32+00:00 = http://api.wayback.archive.org/memento/ 19971010201632/http://www.cs.odu.edu/
  • 11.
    Code Walk Through class TimeMap: Memento timemap container class  class TimeMapTokenizer: Helper class to tokenize a link-style timemap  __main__: Used for quick unit testing Download the code: http://www.cs.odu.edu/~sainswor/uploads/Downloads/timemap.py
  • 12.
    Summary  Easy tolearn  Fast development cycle  Dynamic typing  Extensive standard and add-on libraries  High-level data structures  Widely-supported  Free  Linux/Unix, OS X, Windows
  • 13.
  • 14.
    Links  Slides: http://www.cs.odu.edu/~sainswor/uploads/Downloads/ACM-Python.py Code: http://www.cs.odu.edu/~sainswor/uploads/Downloads/timemap.py  Python: http://www.python.org  Python Docs: http://docs.python.org  Python Libraries: http://pypi.python.org

Editor's Notes

  • #3 Relationship to other languages
  • #5 Quick introduction to the syntax
  • #7 Lists, tuples, dictionaries, &amp; strings
  • #8 Lists, tuples, dictionaries, &amp; strings
  • #13 Relationship to other languages