python programming

Download Python programming

Post on 31-Dec-2015

38 views

Category:

Documents

0 download

Embed Size (px)

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

  • Python programmingIntroduction to the JES environment and basics of PythonReading: Chapters 1, 2 from Introduction to Computing and Programming in Python

  • PythonThe programming language we will be using is called Pythonhttp://www.python.orgIts used by companies like Google, Industrial Light & Magic, Nextel, and othersThe kind of Python were using is called JythonIts Java-based Pythonhttp://www.jython.orgWell be using a specific tool to make Python programming easier, called JES (Jython Environment for Students).

  • JES - Jython Environment for StudentsProgram area- A simple editor for programsCommand area Interaction with Jython

    Program Area

    Command Area

  • Python interaction through commandsAnything you type in command area is evaluated and its value is displayed Example:

    >>> 5 + 38>>> spamspam>>> spamspam>>> spam and more spamspam and more spam>>> spam + spamspamspamprompt

  • 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 + 38>>> print spamspam>>> spam + spamspamspam>>> print spam + spamSpamspam

    Note: no quotes!

  • Command Area EditingUp/down arrows walk through command historyYou can edit the line at the bottomJust put the cursor at the end of the line before hitting Return/Enter.

  • Variables are names for dataExample:a= 3b= -1c = 2x = 0f = a*x*x + b*x + c

  • Variables -more examplesVariables of other typesnewsItem = Youve got spam!Variables keep their value for the duration of a program or until they get a new value through a new assignmenta = 3b = a *2 + 5a = 0Up to this point the value of a is still 3, but then it changes to 0

  • Python functionsPython has a lot of built-in functions for general mathematical manipulation, eg:sqrt(4) - computes the square root of 4ord(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)4aF(4, a)side-effects

  • Functions in generalF(a,b)4aF(4, a)side-effects

  • Example: sqrt()sqrt(a)42side-effects

  • JES FunctionsA bunch of functions are pre-defined in JES for sound and picture manipulationspickAFile()makePicture()makeSound()show()play()Some of these functions accept input valuestheFile = pickAFile()pic = makePicture(theFile)

  • Example: pickAFile()pickAFile()Filename (eg: C:\Documents and Settings\mpapalas\My Documents\greenroom.jpg)Pops up a dialogbox for the user toselect a file

  • Picture FunctionsmakePicture(filename) creates and returns a picture object, from the JPEG file at the filenameshow(picture) displays a picture in a windowWell learn functions for manipulating pictures later, like getColor(), setColor(), and repaint()

  • Example: makePicture()makePicture(filename)Picture object corresponding to image that is saved in theFiletheFilemakePicture(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 theFilePops 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 picPicture, filename C:\Documents and Settings\mpapalas\Desktop\sample.jpg height 1200 width 1600>>> show(pic)

  • Writing a recipe: Making our own functionsdef fcnName (input1, input2,...) :

    block describing what the function should do return value

    To make a function, use the command defThen, 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 blockOptionally, the function can return a value

  • Writing a recipe: Making our own functionsdef fcnName (input1, input2,...) :

    block describing what the function should do return value

    To make a function, use the command defThen, 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 blockOptionally, the function can return a valueOptional

  • pickAFile()Pops up a dialogbox for the user toselect a filetheFile

    A recipe for displaying picked picture filesdef pickAndShow(): theFile = pickAFile() Defines a new function

  • pickAFile()Pops up a dialogbox for the user toselect a filemakePicture(filename)pictheFile

    A recipe for displaying picked picture filesdef pickAndShow(): theFile = pickAFile() pic = makePicture(theFile)

  • pickAFile()Pops up a dialogbox for the user toselect a filemakePicture(filename)pictheFile

    show(picture-obj)Pops up a new window displaying image stored in the the picture objectA recipe for displaying picked picture filesdef pickAndShow(): theFile = pickAFile() pic = makePicture(theFile) show(pic)

  • pickAFile()Pops up a dialogbox for the user toselect a filemakePicture(filename)pictheFile

    show(picture-obj)Pops up a new window displaying image stored in the the picture objectA recipe for displaying picked picture filesdef pickAndShow(): theFile = pickAFile() pic = makePicture(theFile) show(pic)pickAndShow()

  • A recipe for playing picked sound filesdef 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 AreaProgram area- A simple editor for programs Command area Interaction with Jython

    Program Area

    Command Area

  • Using the Program AreaType your program (or cut and paste from the command area) Save (or Save As) - use .py for file extensionLoad 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 functionYou must Save before LoadingYou must Load before you can use your function

  • Making functions the easy wayGet something working by typing commandsEnter 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)Cant contain spacesmyPicture is okay but my Picture is notBe careful not to use command names as your own namesprint = 1 wont work(Avoid names that appear in the editor pane of JES highlighted in blue or purple)Case mattersMyPicture is not the same as myPicture or mypictureSensible names are sensibleE.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 JESStatements 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 areaTry using your function by typing pickAndShow() in the command area

  • Exploring more functionsThe 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?

  • ReadingChapters 1, 2 from Introduction to Computing and Programming in Python

  • AssignmentCreate a function thatCauses an interactive input window to pop up and request some form of input from the user and stores it in a variableDisplays the value stored in the variableDisplays an image from a fileSave your function in a fileLoad itUse the function in the command area to make sure that it works and does what you want it to doSubmit your file through webct

    Do show this live lots of examples, live installation.URL to look at what companies use Python is in the book.Do these interactively. Tell people not to worry too much about the meanings now more on Friday. Just read them now, and the names should make the meaning (at least somewhat) clear.