350 likes | 718 Views
Python programming. Introduction to the JES environment and basics of Python. Reading: Chapters 1, 2 from “Introduction to Computing and Programming in Python”. Python. The programming language we will be using is called Python http://www.python.org
E N D
Python programming Introduction to the JES environment and basics of Python • Reading: Chapters 1, 2 from “Introduction to Computing and Programming in Python”
Python • The programming language we will be using is called Python • http://www.python.org • It’s used by companies like Google, Industrial Light & Magic, Nextel, and others • The kind of Python we’re using is called Jython • It’s Java-based Python • http://www.jython.org • We’ll be using a specific tool to make Python programming easier, called JES (Jython Environment for Students).
JES - Jython Environment for Students Program area - A simple editor for programs • Command area • Interaction with Jython
Python interaction through commands Anything you type in command area is evaluated and its value is displayed • Example: >>> 5 + 3 8 >>> ‘spam’ ‘spam’ >>> “spam” ‘spam’ >>> “spam and more spam” ‘spam and more spam’ >>> ‘spam’ + ‘spam’ ‘spamspam’ prompt
print displays the value of an expression • In many cases it makes no difference whether you use print - the result gets displayed anyway. >>> print 5 + 3 8 >>> print ‘spam’ spam >>> ‘spam’ + ‘spam’ ‘spamspam’ >>> print ‘spam’ + ‘spam’ Spamspam Note: no quotes!
Command Area Editing • Up/down arrows walk through command history • You can edit the line at the bottom • Just put the cursor at the end of the line before hitting Return/Enter.
Variables are names for data Example: a= 3 b= -1 c = 2 x = 0 f = a*x*x + b*x + c
Variables -more examples • Variables of other types newsItem = “You’ve got spam!” • Variables keep their value for the duration of a program or until they get a new value through a new assignment a = 3 b = a *2 + 5 a = 0 Up to this point the value of a is still 3, but then it changes to 0
Python functions • Python has a lot of built-in functions for general mathematical manipulation, eg: • sqrt(4) - computes the square root of 4 • ord(‘A’) - computes the ASCII code for character ‘A’ • Example: print (-b+sqrt(b*b - 4*a*c))/2*a
But what exactly is a function? F(a,b) 4 ‘a’ F(4, ‘a’) side-effects
Functions in general F(a,b) 4 ‘a’ F(4, ‘a’) side-effects
Example: sqrt() sqrt(a) 4 2 side-effects
JES Functions • A bunch of functions are pre-defined in JES for sound and picture manipulations • pickAFile() • makePicture() • makeSound() • show() • play() • Some of these functions accept input values theFile = pickAFile() pic = makePicture(theFile)
Example: pickAFile() pickAFile() Filename (eg: C:\Documents and Settings\mpapalas\My Documents\greenroom.jpg) Pops up a dialog box for the user to select a file
Picture Functions • makePicture(filename) creates and returns a picture object, from the JPEG file at the filename • show(picture) displays a picture in a window • We’ll learn functions for manipulating pictures later, like getColor(), setColor(), and repaint()
Example: makePicture() makePicture(filename) Picture object corresponding to image that is saved in theFile theFile • makePicture(filename) • creates and returns a picture object, from the JPEG file at the filename
Example: show() show(picture-obj) Picture object corresponding to image that is saved in theFile Pops up a new window displaying image stored in the the picture object
Demonstrating picture manipulation with JES >>> >>> print pickAFile() C:\Documents and Settings\mpapalas\Desktop\sample.jpg >>> theFile = "C:\Documents and Settings\mpapalas\Desktop\sample.jpg" >>> makePicture(theFile) Picture, filename C:\Documents and Settings\mpapalas\Desktop\sample.jpg height 1200 width 1600 >>> print makePicture(theFile) Picture, filename C:\Documents and Settings\mpapalas\Desktop\sample.jpg height 1200 width 1600 >>> pic = makePicture(theFile) >>> print pic Picture, filename C:\Documents and Settings\mpapalas\Desktop\sample.jpg height 1200 width 1600 >>> show(pic)
Writing a recipe: Making our own functions def fcnName (input1, input2,...) : block describing what the function should do returnvalue • To make a function, use the command def • Then, the name of the function, and the names of the input values between parentheses (“(input1)”) • Important: End the line with a colon (“:”) • The body of the recipe is indented (Hint: Use two spaces) • That’s called a block • Optionally, the function can return a value
Writing a recipe: Making our own functions def fcnName (input1, input2,...) : block describing what the function should do returnvalue • To make a function, use the command def • Then, the name of the function, and the names of the input values between parentheses (“(input1)”) • Important: End the line with a colon (“:”) • The body of the recipe is indented (Hint: Use two spaces) • That’s called a block • Optionally, the function can return a value Optional
A recipe for displaying picked picture files Defines a new function defpickAndShow(): theFile =pickAFile() pickAFile() theFile Pops up a dialog box for the user to select a file
A recipe for displaying picked picture files defpickAndShow(): theFile =pickAFile() pic =makePicture(theFile) pickAFile() makePicture(filename) theFile pic Pops up a dialog box for the user to select a file
A recipe for displaying picked picture files defpickAndShow(): theFile =pickAFile() pic =makePicture(theFile) show(pic) pickAFile() makePicture(filename) show(picture-obj) theFile pic Pops up a new window displaying image stored in the the picture object Pops up a dialog box for the user to select a file
A recipe for displaying picked picture files defpickAndShow(): theFile = pickAFile() pic = makePicture(theFile) show(pic) pickAndShow() pickAFile() makePicture(filename) show(picture-obj) theFile pic Pops up a new window displaying image stored in the the picture object Pops up a dialog box for the user to select a file
A recipe for playing picked sound files def pickAndPlay(): myfile = pickAFile() mysound = makeSound(myfile) play(mysound) Note:myfile and mysound, inside pickAndPlay(), are completely different from the same names in the command area.
Anything complicated is best done in the Program Area Program area - A simple editor for programs • Command area • Interaction with Jython
Using the Program Area • Type your program (or cut and paste from the command area) • Save (or Save As) - use .py for file extension • Load Program (click on button between command and program areas) • Before you load it, the program is just a bunch of characters. • Loading encodes it as an executable function • You must Save before Loading • You must Load before you can use your function
Making functions the easy way • Get something working by typing commands • Enter the def command. • Copy-paste the right commands up into the recipe
Names for variables and functions can be (nearly)anything! • Must start with a letter (but can contain numerals) • Can’t contain spaces • myPicture is okay but my Picture is not • Be careful not to use command names as your own names • print = 1 won’t work • (Avoid names that appear in the editor pane of JES highlighted in blue or purple) • Case matters • MyPicture is not the same as myPicture or mypicture • Sensible names are sensible • E.g. myPicture is a good name for a picture, but not for a picture file. • x could be a good name for an x-coordinate in a picture, but probably not for anything else
Blocking is indicated for you in JES • Statements that are indented the same, are in the same block. • Statements that are in the same block as where the line where the cursor is are enclosed in a blue box.
Now what? • When you load your program, what happens with the function? • After a program with a function definition is loaded, that function can be used either from the command or the program area • Try using your function by typing pickAndShow() in the command area
Exploring more functions • The JES Functions menu has functions arranged by type - check them out! • Can you find a function that inputs a number? • Can you find under what category pickAFile() is listed?
Reading • Chapters 1, 2 from “Introduction to Computing and Programming in Python”
Assignment Create a function that… • Causes an interactive input window to pop up and request some form of input from the user and stores it in a variable • Displays the value stored in the variable • Displays an image from a file • Save your function in a file • Load it • Use the function in the command area to make sure that it works and does what you want it to do • Submit your file through webct