Title: Python programming
1Python programming
- Introduction to the JES environment and basics of
Python
- Reading Chapters 1, 2 from Introduction to
Computing and Programming in Python
2Python
- The programming language we will be using is
called Python - http//www.python.org
- Its used by companies like Google, Industrial
Light Magic, Nextel, and others - The kind of Python were using is called Jython
- Its Java-based Python
- http//www.jython.org
- Well be using a specific tool to make Python
programming easier, called JES (Jython
Environment for Students).
3JES - Jython Environment for Students
Program area - A simple editor for programs
- Command area
- Interaction with Jython
4Python interaction through commands
- Anything you type in command area is evaluated
and its value is displayed - Example
gtgtgt 5 3 8 gtgtgt spam spam gtgtgt
spam spam gtgtgt spam and more spam spam and
more spam gtgtgt spam spam spamspam
prompt
5print displays the value of an expression
- In many cases it makes no difference whether you
use print - the result gets displayed anyway.
gtgtgt print 5 3 8 gtgtgt print spam spam gtgtgt
spam spam spamspam gtgtgt print spam
spam Spamspam
Note no quotes!
6Command 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.
7Variables are names for data
- Example
- a 3
- b -1
- c 2
- x 0
- f axx bx c
8Variables -more examples
- Variables of other types
- newsItem Youve 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
9Python 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 (-bsqrt(bb - 4ac))/2a
10But what exactly is a function?
F(a,b)
4
a
F(4, a)
side-effects
11Functions in general
F(a,b)
4
a
F(4, a)
side-effects
12Example sqrt()
sqrt(a)
4
2
side-effects
13JES 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)
14Example 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
15Picture Functions
- makePicture(filename) creates and returns a
picture object, from the JPEG file at the
filename - show(picture) displays a picture in a window
- Well learn functions for manipulating pictures
later, like getColor(), setColor(), and repaint()
16Example 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
17Example 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
18Demonstrating picture manipulation with JES
gtgtgt gtgtgt print pickAFile() C\Documents and
Settings\mpapalas\Desktop\sample.jpg gtgtgt theFile
"C\Documents and Settings\mpapalas\Desktop\samp
le.jpg" gtgtgt makePicture(theFile) Picture,
filename C\Documents and Settings\mpapalas\Deskto
p\sample.jpg height 1200 width 1600 gtgtgt print
makePicture(theFile) Picture, filename
C\Documents and Settings\mpapalas\Desktop\sample.
jpg height 1200 width 1600 gtgtgt pic
makePicture(theFile) gtgtgt print pic Picture,
filename C\Documents and Settings\mpapalas\Deskto
p\sample.jpg height 1200 width 1600 gtgtgt show(pic)
19Writing 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) - Thats called a block
- Optionally, the function can return a value
20Writing 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) - Thats called a block
- Optionally, the function can return a value
Optional
21A recipe for displaying picked picture files
Defines a new function
def pickAndShow() theFile pickAFile()
pickAFile()
theFile
Pops up a dialog box for the user to select a file
22A recipe for displaying picked picture files
def pickAndShow() theFile pickAFile() pic
makePicture(theFile)
pickAFile()
makePicture(filename)
theFile
pic
Pops up a dialog box for the user to select a file
23A recipe for displaying picked picture files
def pickAndShow() 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
24A recipe for displaying picked picture files
def pickAndShow() 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
25A 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.
26Anything complicated is best done in the Program
Area
Program area - A simple editor for programs
- Command area
- Interaction with Jython
27Using 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
28Making functions the easy way
- Get something working by typing commands
- Enter the def command.
- Copy-paste the right commands up into the recipe
29Names for variables and functions can be
(nearly)anything!
- Must start with a letter (but can contain
numerals) - Cant contain spaces
- myPicture is okay but my Picture is not
- Be careful not to use command names as your own
names - print 1 wont 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
30Blocking 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.
31Now 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
32Exploring 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?
33Reading
- Chapters 1, 2 from Introduction to Computing and
Programming in Python
34Assignment
- 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