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 ours How high can we rasie 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
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 Enter a tab to set off the body of the loop, which is the part that gets repeated. Type a return at the end of the body.
One More For You A second type of loop specifies a range of values for a variable to take on Why did it just go to 9?
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)
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 first character is 0) Strings can be sliced x[2:7] to get a range of characters
You ve seen range() Loops with Strings Another built in function is len()
A Function Let s try some simple user-defined functions Two levels of indention. One for the body of the function, another for the subbody of the for loop 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 executed conditionally Can use and and or to combine conditions
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
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 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
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
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
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 the game in reverse, where the computer tries to guess your number
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.
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