28
Lecture # 27 Python I

Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Embed Size (px)

Citation preview

Page 1: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Lecture # 27

Python I

Page 2: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

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.

Page 3: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython

• Jython is a “dialect” of Python

• Python is implemented in CJython is implemented in Java

• We use JES JES = Jython Environment for Students

Page 4: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Downloading Jython

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

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

Page 5: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Using Jython

• Select JES from “Programs”

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

Page 6: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython DEMO

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

Page 7: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

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.

Page 8: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython DEMO Pictures• pickAFile()

Page 9: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython DEMO Pictures• pickAFile()

• print pickAFile()

Page 10: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython DEMO Pictures• pickAFile()

• print pickAFile()

• file = pickAFile()

Page 11: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython DEMO Pictures• pickAFile()

• print pickAFile()

• file = pickAFile()

• print file

Page 12: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython DEMO Pictures• pickAFile()

• print pickAFile()

• file = pickAFile()

• print file

• pict=makePicture(file) # interpret as picture

Page 13: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython DEMO Pictures• pickAFile()

• print pickAFile()

• file = pickAFile()

• print file

• pict=makePicture(file) # interpret as picture

• show(pict) # display as picture

Page 14: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

How could we do this all at once?

Page 15: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

How could we do this all at once?

show(makePicture(pickAFile()))

Page 16: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

How could we do this all at once?

show(makePicture(pickAFile()))

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

Page 17: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

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

Page 18: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

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”

Page 19: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

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

Page 20: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython Functions

def myFunction(): colon

Page 21: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython Functions

def myFunction(): colon

parentheses

Page 22: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython Functions

def myFunction(): colon

parentheses function name

Page 23: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython Functions

def myFunction(): colon

parentheses function name

“def” instead of “function()”

Page 24: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython Functions

def myFunction(): colon

parentheses function name

“def” instead of “function()”

The function must be called somewhere

Page 25: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython Functions

def myFunction():

block of commands

Page 26: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython Functions

def myFunction():

block of commands

Note indentation!

Page 27: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython Functions: Indentation

• Indentation is critical in Python

• Indentation associates these commands with this function

• Indentation separates commands from other functions

Page 28: Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a

Jython Function: Example

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

pickAndShow() # somewhere we have to call the function