Python MiniCourse - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Python MiniCourse

Description:

Syntax Reminders #!/usr/local/bin/python. import sys. z = complex(1.0,1.0) print z. print ' ... usr/local/bin/python. class Startibartfast: def hello(self) ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 11
Provided by: KenHa98
Category:
Tags: minicourse | python | usr

less

Transcript and Presenter's Notes

Title: Python MiniCourse


1
Python Mini-Course
  • Part 2 Object-Oriented Python
  • Prof Ken Hawick, 2009

2
Syntax Reminders
  • !/usr/local/bin/python
  • import sys
  • z complex(1.0,1.0)
  • print z
  • print '------------------
  • t (1.0,)
  • t (3.0,)
  • print t
  • l 1,2,3
  • l2 7,8,9
  • l l2
  • print l
  • d "apples"1, "pears"42, "bananas" 0
  • print d"apples"
  • print "a" "b",
  • print "Hahha"
  • assert 1 gt 2, foobar"
  • print gtgt sys.stdout, '------------------'

3
More Python Syntax Examples
  • def myfunc(a,b,c)
  • return a b c
  • f myfunc( "apples", "", "pears" )
  • x myfunc( 1,2,3)
  • print f, x
  • print "Hello Ken, enter some text"
  • s raw_input("gt ")
  • gets line from std input with prompt
  • print s
  • print float(s)
  • causes an error if not a float number

4
Object-Oriented Python
  • class classname block
  • class classname(base-classes) block
  • anInstance MyClass()
  • def myMethod(self,nextParameter) block
  • __init__ is constructor method
  • __name__ is classname identifier string
  • __bases__ is tuple of base class objects
  • __doc__ is the docstring

5
Python Class Example
  • !/usr/local/bin/python
  • class Fred defines the class
  • x 42.0
  • def bill(self) first arg is always
    object's self-reference
  • print "Hello from Bill\n" str( Fred.x
    )
  • -------------------------------------------------
    --------------------------------------------------
    --
  • f Fred() instantiates a Fred
  • f.bill() invokes the bill method

6
  • !/usr/local/bin/python
  • -------------------------------------------------
    ------------
  • class Startibartfast
  • def hello(self)
  • print "Hello from Slartibartfast's hello
    method"
  • -------------------------------------------------
    ------------
  • class Arthur
  • def hello(self)
  • print "Hello from Arthur's hello
    method\n"
  • return None
  • def doSomething(self, a )
  • print "this is Arthur's doSomething
    method\n" str(a)
  • return None
  • -------------------------------------------------
    ------------
  • class Ford( Startibartfast, Arthur )
  • x 42.0
  • def prefect(self)
  • print "Hello from prefect\n" str(
    Ford.x )
  • -------------------------------------------------
    ------------
  • f Ford()
  • f.prefect()
  • f.hello()
  • f.doSomething( 12345.001 )
  • gt python test02.py
  • Hello from prefect
  • 42.0

7
Python has Multiple Inheritance
  • Supposed to work left to right etc depending on
    order of base classes search order is BFS
  • Built-in object type is ancestor of all objects
    and has some special methods defined.

8
Python Exceptions
  • try
  • statement(s)
  • except expression , target
  • statement(s)
  • else
  • statement(s)
  • finally
  • statement(s)
  • Target is optional identifier name of the
    exception
  • ZeroDivisionError TypeError

9
Other Python factoids
  • Large library of special methods
  • Lots of modules you can download
  • Python is good for
  • Systems admin tasks user interfaces XML
    network programming web programming distributed
    programming introspective programming.
  • It also supports some functional and logic
    programming ideas

10
More Information on Python
  • Python in a Nutshell , Alex Martelli, Pub.
    OReilly
  • Python Cookbook, Alex Martelli, Anna Martelli
    Ravenscroft and David Ascher, Pub. OReilly
  • http//www.python.org
  • http//docs.python.org/tutorial/
Write a Comment
User Comments (0)
About PowerShow.com