Presentation is loading. Please wait.

Presentation is loading. Please wait.

You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.

Similar presentations


Presentation on theme: "You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in."— Presentation transcript:

1 You Need an Interpreter!

2 Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke 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 what instructions to execute, where variables are stored, and how best to accomplish the goals of the program given a high-level specification

3 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

4 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

5 Feeling Loopy? Let’s write a simple loop to repeat a command several times

6 One More “For” You A second type of loop specifies a range of values for a variable to take on

7 What’s in a “Range” A “Range” generates a “list” of values to set a variable to. Three versions range(whileLessThan) # starts at ‘0’ range(first, whileLessThan) range(first, whileLessThan, step)

8 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

9 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 first character is 0) Strings can be “sliced” x[2:7] to get a range of characters

10 Loops with Strings You’ve seen “range()” Another built in function is len()

11 A Function Let’s try some simple user-defined functions Functions can call other functions

12 More Math By default, integer arithmetic is used on integers (might lead to unexpected results) More functions

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

14 Conditionals “if/then/else” allow statements to be executed conditionally Can use “and” and “or” to combine conditions

15 From Statements to a Program Thus far, we’ve written small functions and entered simple statements into the python interpreter. At this point we’ll store a list of statements 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

16 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

17 Creating a File 1) On the “File” Menu Choose “New Window” 2) Type in the simple function and the following for loop Notice how Idle automatically highlights keywords 3) When finished save the file as “HexDigits.py”

18 Running the Program 1) On the “Run” Menu Choose “Run Module” or just press “F5” 2) The program should run in the shell window 3) You can also run it by double-clicking on the file in an explorer window

19 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 interesting programs that interact with us

20 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

21 The Reverse Here’s the game in reverse, where the computer tries to guess your number

22 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 into a list separated by characters from the “control” string. If no control string is specified, the string “string.whitespace” from the string library is used.

23 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


Download ppt "You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in."

Similar presentations


Ads by Google