Date: Thursday, January 10, 2008.
Note. Open the following link in a new tab or window:
http://docs.python.org/ref/ref.html.
You can use it as a reference whenever you want to learn more about
any of the thing you try in the tutorial.
Ex. 1. a. Python console.
Open a terminal and type the command python. You should see the prompt change to ">>>" which means that the Python interpreter has been launched.
Type
print "Hello world"and then enter.
Go back to the previous command using the up arrow key (like in the shell) and edit the string to make two strings from it for the two words and separate it by a comma. You can use the left-right arrows for the editing. Execute this command.
Define a variable called name and assign it your first name as a string. For example, in my case I would write
name = "Dana"
Go back to the second printing of the Hello world and replace the second string with the variable name (without the quotes). Execute the command.
b. Python as a calculator.
Type an arithmetic expression without even assigning it or printing it, as for example, 12.7 - 31/5, then execute it. Try different combinations of operators and constants.
Importing a library: type the following command:
from math import *
Try various combinations of mathematical functions, as for example cos(2.0/3*pi) or log(10).
c. Executing a loop. Type the following:
for i in range(4):Note what happens after you press enter. The interpreter waits for you to continue. Start the next instruction with a tab, and then type:
print iYou will need to press enter twice to tell Python that the body of the loop has ended.
Repeat the procedure and add a comma at the end of the printing statement for i. Also try a range with two values separated by a comma. Exit the interpreter with Ctrl-D.
Ex. 2. Open an emacs window to edit a buffer called lab1.py. You should see two menus appearing at the top of Emacs called IM-Python and Python. The first one will eventually contain shortcuts to all the functions/classes in the file that you're editing. The second one contains some useful Python operations.
a. Writing and executing a small program. Start the program with a comment (each comment line must start with "#") containing your name and the lab number. The final result of this file will be part of the homework, so you'll be sending me this file at some point.
Type the following instructions:
print "Stapler before dawn" print "Waiting in perfect silence" print "Dreams of loose paper"
Note: poem from http://www.virtualstapler.com/, author
C. Allen. Save the file and return to the terminal. Run the script
with the following command:
python lab1.py
Add the following line as the very first thing in the file and save
it:
#!/usr/bin/python
Go back to the terminal and give yourself permission to execute the
file:
chmod u+x lab1.py
Execute the script again with the command
lab1.py
b. Adding variables. Replace the 3 print instructions with the names of 3 variables like word1, word2, word3, followed by the = to convert these printing statements into assignments. Add a fourth instruction that prints the 3 variables separated by commas and execute the script.
The end of lines seem to have disappeared. To put them back, let's store the entire poem in a variable:
poem = word1 + "\n" + word2 + "\n" + word3Print this new variable instead.
c. Function. Let's turn this code into a function by adding the following line before:
def words():All of the other instructions should be bellow this one and you'll have to indent them by a tab. If you place the cursor anywhere on these lines and press the tab, it will indent the whole line. Once you're done, execute the script again. What happens and why?
We need to add a call to this function. Add two empty lines after the last instruction. If you started with the cursor on the last line, emacs will automatically indent that line the new lines at the same level as the instruction above. We need the function call to be outside of the function, so to close the function body, press the Backspace. The minibuffer should tell you that it closed that block.
The function call is simply
words()
d. String operations. Copy and paste the function as a new one. If at this point you've seen enough of the initial poem, replace the 3 lines of text with anything else you want. Call this function words_array.
Add the following line at the top of the file after the comments:
from string import *Add the following line in the words_array function after you define the poem variable:
sentence = split(poem, "\n")Print this variable instead of the poem and change the function call to the new one.
e. Adding a loop. Add the following instructions after the print statement:
for w in sentence: print wNote that the second instruction should be indented one tab more than the first one. Execute the script again.
Add the following instructions:
print join(sentence, "\n\n") pos = find(poem, word2) print "the second line starts at position", pos, "in the string"or any other separator ("\n\n") that you think might be interesting for the first one. Execute it to see the result.
Go to the IM-Python menu and click on *Rescan*. The two function names should appear in the menu. Note what happens when you click on one of them.
f. Random. Make another copy of the first function and call it words_random. Add another import line after the one for the string, and import the module random.
In the function words_random, replace the separator "\n" in the definition of the variable poem with a simple space. Copy the split instruction from the function words_array and replace the separator again with a space. Add the following lines:
for i in range(len(sentence)): sentence[i] = lower(sentence[i])Print the result. Replace the function call with the new one and execute the script.
Add a second loop in this function identical to the first one. On a line before the loop, declare a variable L and store the value of len(sentence) in it. Replace this function call with this new variable in the loop.
As the body of the loop, add the following lines:
j = randint(0, L-1) print sentence[j],Comment out the printing of the whole sentence. Save the file and execute it.
g. A conditional. On the line before the print statement in the loop add the following:
if i==0 :Indent the printing statement to be inside this conditional and make a copy of it. Add
else:in between the two printing statements.
Add a function call to the function capitalize around sentence[j] (not including the comma) in the first printing statement. Save the file and execute it. Try it again a couple of times to see if the result is different. This is the end of the lab.
Result. This file is the first part of the homework and you must send it to me. The second part will be posted on the web site later.