python programming

34
Python programming Introduction to the JES environment and basics of Python Reading: Chapters 1, 2 from “Introduction to Computing and Programming in Python”

Upload: clark-dickerson

Post on 31-Dec-2015

100 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Python programming

Python programming

Introduction to the JES environment and basics of Python

•Reading: Chapters 1, 2 from “Introduction to Computing and Programming in Python”

Page 2: Python programming

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).

Page 3: Python programming

JES - Jython Environment for Students

Program Area

Command Area

Program area- A simple editor for programs

Command area- Interaction with Jython

Page 4: Python programming

>>> 5 + 38>>> ‘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

Page 5: Python programming

>>> print 5 + 38>>> 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!

Page 6: Python programming

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.

Page 7: Python programming

Variables are names for data

Example:

a= 3

b= -1

c = 2

x = 0

f = a*x*x + b*x + c

Page 8: Python programming

Variables -more examples

• Variables of other typesnewsItem = “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

Page 9: Python programming

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

Page 10: Python programming

But what exactly is a function?

F(a,b)

4

‘a’ F(4, ‘a’)

side-effects

Page 11: Python programming

Functions in general

F(a,b)

4

‘a’ F(4, ‘a’)

side-effects

Page 12: Python programming

Example: sqrt()

sqrt(a)4 2

side-effects

Page 13: Python programming

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)

Page 14: Python programming

Example: pickAFile()

pickAFile() Filename (eg: C:\Documents and Settings\mpapalas\My Documents\greenroom.jpg)

Pops up a dialogbox for the user to

select a file

Page 15: Python programming

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()

Page 16: Python programming

Example: makePicture()

makePicture(filename)Picture object corresponding to

image that is saved in theFiletheFile

makePicture(filename)• creates and returns a picture object, from the JPEG file at the filename

Page 17: Python programming

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

Page 18: Python programming

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 picPicture, filename C:\Documents and Settings\mpapalas\Desktop\sample.jpg height 1200 width 1600>>> show(pic)

Page 19: Python programming

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

Page 20: Python programming

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

Page 21: Python programming

pickAFile()

Pops up a dialogbox for the user to

select a file

theFile

A recipe for displaying picked picture files

def pickAndShow(): theFile = pickAFile()

Defines a new function

Page 22: Python programming

pickAFile()

Pops up a dialogbox for the user to

select a file

makePicture(filename)pictheFile

A recipe for displaying picked picture files

def pickAndShow(): theFile = pickAFile() pic = makePicture(theFile)

Page 23: Python programming

pickAFile()

Pops up a dialogbox for the user to

select a file

makePicture(filename)pictheFile

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)

Page 24: Python programming

pickAFile()

Pops up a dialogbox for the user to

select a file

makePicture(filename)pictheFile

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()

Page 25: Python programming

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.

Page 26: Python programming

Anything complicated is best done in the Program Area

Program Area

Command Area

Program area- A simple editor for programs

Command area- Interaction with Jython

Page 27: Python programming

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

Page 28: Python programming

Making functions the easy way

• Get something working by typing commands

• Enter the def command.

• Copy-paste the right commands up into the recipe

Page 29: Python programming

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

Page 30: Python programming

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.

Page 31: Python programming

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

Page 32: Python programming

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?

Page 33: Python programming

Reading

• Chapters 1, 2 from “Introduction to Computing and Programming in Python”

Page 34: Python programming

AssignmentCreate 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