Download presentation
Presentation is loading. Please wait.
Published byDinah Harrison Modified over 9 years ago
1
Python programming Introduction to the JES environment and basics of Python Reading: Chapters 1, 2 from “Introduction to Computing and Programming in Python”
2
Python The programming language we will be using is called Python –http://www.python.orghttp://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.orghttp://www.jython.org We’ll be using a specific tool to make Python programming easier, called JES (Jython Environment for Students).
3
JES - Jython Environment for Students Program area - A simple editor for programs Command area - Interaction with Jython
4
>>> 5 + 3 8 >>> ‘spam’ ‘spam’ >>> “spam” ‘spam’ >>> “spam and more spam” ‘spam and more spam’ >>> ‘spam’ + ‘spam’ ‘spamspam’ Python interaction through commands Anything you type in command area is evaluated and its value is displayed Example: prompt
5
>>> print 5 + 3 8 >>> print ‘spam’ spam >>> ‘spam’ + ‘spam’ ‘spamspam’ >>> print ‘spam’ + ‘spam’ Spamspam print displays the value of an expression In many cases it makes no difference whether you use print - the result gets displayed anyway. Note: no quotes!
6
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.
7
Variables are names for data Example: a= 3 b= -1 c = 2 x = 0 f = a*x*x + b*x + c
8
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
9
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
10
But what exactly is a function? F(a,b) 4 ‘a’F(4, ‘a’) side-effects
11
Functions in general F(a,b) 4 ‘a’F(4, ‘a’) side-effects
12
Example: sqrt() sqrt(a) 4 2 side-effects
13
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)
14
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
15
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()
16
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
17
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
18
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)
19
Writing a recipe: Making our own functions def fcnName (input1, input2,...) : block describing what the function should do return value 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
20
Writing a recipe: Making our own functions def fcnName (input1, input2,...) : block describing what the function should do return value 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
21
pickAFile() Pops up a dialog box for the user to select a file theFile A recipe for displaying picked picture files def pickAndShow(): theFile = pickAFile() Defines a new function
22
pickAFile() Pops up a dialog box for the user to select a file makePicture(filename) pic theFile A recipe for displaying picked picture files def pickAndShow(): theFile = pickAFile() pic = makePicture(theFile)
23
pickAFile() Pops up a dialog box for the user to select a file makePicture(filename) pic theFile show(picture-obj) Pops up a new window displaying image stored in the the picture object A recipe for displaying picked picture files def pickAndShow(): theFile = pickAFile() pic = makePicture(theFile) show(pic)
24
pickAFile() Pops up a dialog box for the user to select a file makePicture(filename) pic theFile show(picture-obj) Pops up a new window displaying image stored in the the picture object A recipe for displaying picked picture files def pickAndShow(): theFile = pickAFile() pic = makePicture(theFile) show(pic) pickAndShow()
25
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.
26
Anything complicated is best done in the Program Area Program area - A simple editor for programs Command area - Interaction with Jython
27
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
28
Making functions the easy way Get something working by typing commands Enter the def command. Copy-paste the right commands up into the recipe
29
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
30
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.
31
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
32
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?
33
Reading Chapters 1, 2 from “Introduction to Computing and Programming in Python”
34
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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.