You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in...

23
You Need an Interpreter!

Transcript of You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in...

You Need an Interpreter!

Closing the GAP

• Thus far, we’ve been struggling to speak tocomputers in “their” language, maybe its time wespoke to them in ours

• How “high” can we raise the level of discourse?• We’ll enlist the aid of an interpreter• An “interpreted” language uses the computer to

do all the hard work of figuring out what is“meant” by a statement. It figures out whatinstructions to execute, where variables arestored, and how best to accomplish the goals ofthe program given a high-level specification

A Quick Class Exercise

• Download Python 2.5 (www.python.org/download)

• Most of you will click on the “Windows Installer”• Select the defaults and install on your machine• Now choose“ Python (command line)”

from your start menu

Getting Started

• Type the following commands at the “>>>” prompt

• After entering a couple of commands, try using the “up-arrow” key to bring back an old command

Feeling Loopy?

• Let’s write a simple loop to repeat a command several times

One More “For” You

• A second type of loop specifies a range of values for a variable to take on

What’s in a “Range”

• A “Range” generates a “list” of values to set a variable to.

• Three versionsrange(whileLessThan) # starts at ‘0’range(first, whileLessThan)range(first, whileLessThan, step)

Or Make your own List

• A “for” loop will replace the variable with the items from any user-specified list

• Lists can hold numbers, “strings”, or variables

No Strings Attached

• Variables can be set to a sequence of characters called“strings”

• Python provides many ways of processing strings• Strings can be “indexed” x[0] to access characters

(the firstcharacteris 0)

• Strings canbe “sliced”x[2:7]to get a rangeof characters

Loops with Strings

• You’ve seen “range()”• Another built in function is len()

A Function

• Let’s try some simple user-defined functions

• Functions can call other functions

More Math

• By default, integer arithmetic is used on integers (might lead to unexpected results)

• More functions

More Lists• Lists are very flexible variable types• Can be “indexed” and “sliced” like strings• Also, sorted, modified, shortened, an enlarged

Conditionals• “if/then/else” allow statements to be executedconditionally• Can use “and” and “or” to combine conditions

From Statements to a Program

• Thus far, we’ve written small functions andentered simple statements into the pythoninterpreter. At this point we’ll store a list ofstatements in a file to create a program.

• Steps– Create a list of statements in a file with the

filename extension “.py” using a text editor– Run the program one of 2 ways

1) Type “python programname.py”2) Double click on the file

A Simple Text Editor

• Python provides a simple text editor called “Idle”• Operating system also provides a simple editor

(Windows XP: Notepad, Mac OSX: TextEdit)• Idle

+ Python Aware, and thus tries to help you with your program- It does not conform to user-interface standards

Creating a File

1) On the “File” MenuChoose “New Window”

2) Type in the simplefunction and thefollowing for loopNotice how Idleautomaticallyhighlights keywords

3) When finished savethe file as“HexDigits.py”

Running the Program

1) On the “Run” MenuChoose “Run Module”or just press “F5”

2) The program shouldrun in the shellwindow

3) You can also runit by double-clickingon the file in anexplorer window

Getting User Input

• We used a new built in function at the end of this program called raw_input()

• raw_input(“prompt string”) prints the prompt and then reads an input string from the user

• In our example we ignored the input string, but we could have saved it in a variable. Ex.x = raw_input(“What is your name?”)

• This will allow us to write more interestingprograms that interact with us

A Simple Game

• Let’s use what we’ve learned so far to write a simple number guessing game

• Save as“YouGuess.py”

• Run using F5

The Reverse

• Here’s thegame inreverse,where thecomputertries toguess yournumber

A Useful Utility• A simple program to count the number of lines, words, and characters

in a text file• Importing “sys”

and “string”libraries

• String.split(control)splits a string intoa list separated bycharacters fromthe “control” string.If no control stringis specified, thestring“string.whitespace”from the stringlibrary is used.

Next Time

• Now that we can write a “script” or “program” by saving our statements in a file

• We’ll explore more Python• We’ll use Python to explore our

computer• Composing and importing scripts