lecture # 27 python i. python “interpretive” language (vs. compiled) so is html, javascript....

Post on 29-Dec-2015

243 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Lecture # 27

Python I

Python

• “Interpretive” language (vs. compiled)So is HTML, JavaScript.

• Python supports file I/O. JavaScript doesn’t

• Python is a serious web programming language used by Google, ILM, etc.

Jython

• Jython is a “dialect” of Python

• Python is implemented in CJython is implemented in Java

• We use JES JES = Jython Environment for Students

Downloading Jython

• http://code.google.com/p/mediacomp-jes/

• http://code.google.com/p/mediacomp-jes/downloads/list

Using Jython

• Select JES from “Programs”

• Click on the black part of the UI after the command prompt >>>

Jython DEMO

• print 5 + 9• print “Hello”• x = 5 + 9• print x• message = “Hello There”• print message

Jython DEMO

• print 5 + 9• print “Hello”• x = 5 + 9• print x• message = “Hello There”• print message

• Note: Python is case sensitive.“Print” and “Message” won’t work.

Jython DEMO Pictures• pickAFile()

Jython DEMO Pictures• pickAFile()

• print pickAFile()

Jython DEMO Pictures• pickAFile()

• print pickAFile()

• file = pickAFile()

Jython DEMO Pictures• pickAFile()

• print pickAFile()

• file = pickAFile()

• print file

Jython DEMO Pictures• pickAFile()

• print pickAFile()

• file = pickAFile()

• print file

• pict=makePicture(file) # interpret as picture

Jython DEMO Pictures• pickAFile()

• print pickAFile()

• file = pickAFile()

• print file

• pict=makePicture(file) # interpret as picture

• show(pict) # display as picture

How could we do this all at once?

How could we do this all at once?

show(makePicture(pickAFile()))

How could we do this all at once?

show(makePicture(pickAFile()))

Now lets save it as a Python Program:displayPict.py

How could we do this all at once?

show(makePicture(pickAFile()))

Now lets save it as a Python Program:displayPict.py

And run it:open itand load it

How could we do this all at once?

show(makePicture(pickAFile()))

Now lets save it as a Python Program:displayPict.py

And run it:open itand load it King of the “1-liners”

Jython DEMO Sound• file = pickAFile()

• print file

• sound=makeSound(file) # interpret as sound

• play(sound) # lets listen to it

All together: play(makeSound(pickAFile()))

• Save as a Python program and run it

Jython Functions

def myFunction(): colon

Jython Functions

def myFunction(): colon

parentheses

Jython Functions

def myFunction(): colon

parentheses function name

Jython Functions

def myFunction(): colon

parentheses function name

“def” instead of “function()”

Jython Functions

def myFunction(): colon

parentheses function name

“def” instead of “function()”

The function must be called somewhere

Jython Functions

def myFunction():

block of commands

Jython Functions

def myFunction():

block of commands

Note indentation!

Jython Functions: Indentation

• Indentation is critical in Python

• Indentation associates these commands with this function

• Indentation separates commands from other functions

Jython Function: Example

def pickAndShow():file = pickAFile()myPict = makePicture(file)show(myPict)

pickAndShow() # somewhere we have to call the function

top related