And Now For Something Completely Different’

Download Report

Transcript And Now For Something Completely Different’

‘And Now For Something
Completely Different’
Python Programming
David Hartwell Clements
History of the Snake
• c. 1990 Guido van Rossum created Python
as a descendant to ABC that would appeal
to UNIX/C hackers.
• Named after Monty Python’s Flying Circus.
• Python “bridges the gap” between system
and scripting languages.
6 December 2000
Copyright © 2000 David Hartwell
Clements, See Notes for Details
2
Languages/Paradigms Studied in
MACS400
• Prolog
– Logical
• C/C++
– Iterative
• Java
– Object Oriented
• LISP (Lots of Insanely Stupid Parentheses)
– Functional
6 December 2000
Copyright © 2000 David Hartwell
Clements, See Notes for Details
3
So What is Python?
• “Python is an interpreted, object-oriented,
high-level programming language with
dynamic semantics.”
• Python is flexible, powerful, and extremely
easy to use.
• Python is 100% Open Source.
• Python has elements of Functional,
Iterative, and Object Oriented Paradigms.
6 December 2000
Copyright © 2000 David Hartwell
Clements, See Notes for Details
4
Advantages
• Short Code
– 3 to 5 times shorter than Java!
– 5 to 10 times shorter than C++!
•
•
•
•
•
Extensible using C
‘Executable Pseudocode’
Rapid Prototyping
Heavily Cross-Platform (Un*x, MacOS, &c.)
Object Oriented
6 December 2000
Copyright © 2000 David Hartwell
Clements, See Notes for Details
5
Object Orientation, “Done Right”
•
•
•
•
•
Full, Non-Mandated, Support
Classes
Inheritance and Multiple Inheritance
Virtual Methods (pure and otherwise)
Operator Overloading
6 December 2000
Copyright © 2000 David Hartwell
Clements, See Notes for Details
6
An Iterative Example
answer = ‘’ //default it out
while answer != “yellow”:
name = raw_input(“What is your name? ”)
quest = raw_input(“What is your quest? ”)
answer = raw_input(“What is your favorite color? ”)
if answer != “yellow”:
print “Auuuuuuuuugh”
print “Right. Off you go.”
6 December 2000
Copyright © 2000 David Hartwell
Clements, See Notes for Details
7
A Functional Example
import shrubbery #This is how we include
def function1(x,y):
return x * y
function2 = lambda x, y: x + y #in-line comments
print apply( function1, (3, 5) )
for y in map( function2, (2,3,5), (7,11,13) ): print y
6 December 2000
Copyright © 2000 David Hartwell
Clements, See Notes for Details
8
An Object Oriented Example
class Snake:
…
class Python( Snake ):
def __add__( self, other ):
self.hunt( other )
…
def hunt( target ):
…
MyPython, MyAsp = Python(), Snake()
6 December 2000
Copyright © 2000 David Hartwell
Clements, See Notes for Details
9
Getting More Information
• Any O’Reilly (Animal) book, particularly
Programming Python (python on the cover)
and Learning Python (rat on the cover).
• http://www.python.org
6 December 2000
Copyright © 2000 David Hartwell
Clements, See Notes for Details
10