Python Excercise One
Alright! Our first excercise! This is where the rubber hits the road. Where the nose goes to the grind, and whatever other cliche you want to use.
Excercise One—so excited!
[quote]1. Write an English sentence with understandable semantics but incorrect syntax. Write another English sentence which has correct syntax but has semantic errors.[/quote] [note] i think i Will go to disney Land yesterday;.[/note] [note]Enter one of many possible cliche’s that fit this bill.[/note] [quote]2. Start a Python shell. Type 1 + 2 and then hit return. Python evaluates this expression, prints the result, and then prints another prompt. * is the multiplication operator, and ** is the exponentiation operator. Experiment by entering different expressions and recording what is printed by the Python interpreter. What happens if you use the / operator? Are the results what you expect? Explain..[/quote] [note] SCREEEEEEECH!!!! Hold up…do what to who…when? Looks like I am going to have to dig a little deeper before I take this one on.[/note]Okay….apparently I have to go download Python first (15.5MB file size). Done!
Here is what the .txt file says:
[quote]This package will install Python 3.2.2 for Mac OS X
10.6 or later for the following architecture(s):
i386, x86_64.
Installation requires approximately 70 MB of disk space,
ignore the message that it will take zero bytes.
You must install onto your current boot disk, even though the
installer does not enforce this, otherwise things will not work.
Python consists of the Python programming language interpreter, plus
a set of programs to allow easy access to it for Mac users including
an integrated development environment, IDLE, plus a set of pre-built
extension modules that open up specific Macintosh technologies to
Python programs.
**** IMPORTANT ****
Before using IDLE or other programs using the tkinter graphical user
interface toolkit, visit http://www.python.org/download/mac/tcltk/
for current information about supported and recommended versions
of Tcl/Tk for this version of Python and Mac OS X.
*******************
The installer puts applications, an “Update Shell Profile” command,
and a link to the optionally installed Python Documentation into the
“Python 3.2” subfolder of the system Applications folder,
and puts the underlying machinery into the folder
/Library/Frameworks/Python.framework. It can
optionally place links to the command-line tools in /usr/local/bin as
well. Double-click on the “Update Shell Profile” command to add the
“bin” directory inside the framework to your shell’s search path.
More information on Python in general can be found at
http://www.python.org.[/quote]
Okay…so I got the download done and have read the .txt file now to install this is what I see when I begin the install:
Okay…you know the drill…go through the whole install rigamarole hitting continue until it is all said and done.
…And finally we get this…
The whole process including taking the screen shots and editing in photoshop took all of 5 minutes.
Now…to see how this baby works….ON WITH THE LESSON!
Where were we?? Ohhhh…Yes:
[quote]2. Start a Python shell. Type 1 + 2 and then hit return. Python evaluates this expression, prints the result, and then prints another prompt. * is the multiplication operator, and ** is the exponentiation operator. Experiment by entering different expressions and recording what is printed by the Python interpreter. What happens if you use the / operator? Are the results what you expect? Explain..[/quote]
DONE!
[note]Hey that worked! No PEBKAC Errors encountered here![/note]On to the next step:
[quote]3. Type 1 2 and then hit return. Python tries to evaluate the expression, but it can’t because the expression is not syntactically legal. Instead, it prints the error message:[/quote]And that is what I go on my screen….PROGRESS
[note]By the way…if the opposite of “pro” is “con” doesn’t it follow that the opposite of “progress” is “congress”? I’m just saying….[/note] Next….
[quote]4.Type print ‘hello’. Python executes this statement, which has the effect of printing the letters h-e-l-l-o. Notice that the quotation marks that you used to enclose the string are not part of the output. Now type "hello" and describe your result. Make note of when you see the quotation marks and when you don’t.[/quote] [note]And here it is….the first PEBKAC I have encountered….or is it a bug??? HMMMMMM…[/note] Basically, I tried using the >>>print ‘hello’ in various ways, with space, with quote, without quote, just about every combination I could think of and got a bunch of syntax errors…but when I just wrote ‘hello’ in single quotes something happened.
Okay….maybe that was part of the exercise…who knows…anyways the show must go on.
[quote]Type print cheese without the quotation marks. The output will look something like this:[/quote]
Worked like a charm! This is an example of a run-time error since the word cheese is not defined.
[quote]Type 'This is a test...' at the Python prompt and hit enter. Record what happens.
Now create a python script named test1.py with the following contents (be sure to save it before you try to run it):
What happens when you run this script? Now change the contents to:
and run it again.
What happened this time?[/quote]
Then I remembered…
[quote]Alternatively, you can write a program in a file and use the interpreter to execute the contents of the file. Such a file is called a script. For example, we used a text editor to create a file named firstprogram.py with the following contents:
print 1 + 1
By convention, files that contain Python programs have names that end with .py.
To execute the program, we have to tell the interpreter the name of the script:
$ python firstprogram.py 2[/quote]