COMP 150 Lab 1 -- Starting With Python
Lab Directory
Course
Home Page
Task 0, Set up for DH 342
These instructions are skewed toward DH 342 where Windows XP is
running, and Python 2.4 is loaded in the default location, c:\Python24.
If you are not on a Windows computer, see Zelle, Appendix B,
Other Platforms, page 486. Set up a folder for you Python
programs and make it so you can start Idle. Linux machines
generally have Python already installed. Skip to Task 1.
1. First you need to set up a location for your
files. If
you do not have your own notebook computer or a flash drive, see the
discussion of Flash
Drives and Storage in the Syllabus,
and seriously consider getting one. If you are on a Windows
computer, follow just one of the three steps below that is appropriate
for your situation:
- If
you are at your own computer with you,
you can set up a folder for your Python programs anywhere you like.
I suggest you create a dedicated folder, not just
the
Desktop. You might call it python. Create it and
open that
folder. This will be your Python folder in later discussion.
You
can ignore instructions in future labs to copy in old work or save new
work. You should have installer Python to continue.
- If
you have a flash drive:
plug it into the computer. On the machines in 342
DH, there is one USB port in
the front and many on the back of the computer.
Unfortunatelt the one on the front is well hidden. Open the
front plastic cover saying Dell, see the retangular projection.
Underneath is a plug, at almost a vertical angle. Plig in
your flash drive carefully. (Again
I
strongly suggest a long string connected to belongings you will not
forget.) Open My Computer (on the desktop) to see what where the
flash drive
is
mounted, and open that drive. Make a directory for your
Python
programs -- I suggest calling it python. Change to that
directory
and leave the folder open. This will be your Python folder in
later discussion.
- If you do not have a flash
drive and you are at a
lab computer: Open My Computer from the desktop, and then D:.
Create a
folder with your name or initials to make it easy to save and remove
things. Change to that folder. This will be your
(temporary) Python
folder in the later discussion.
2. Mkae sure you have a web browser open. Firefox is on the desktop in 342 DH
and
also in the Loyola software under Internet. It is a safer
browser
than Internet Explorer.
3. Open another tab in Firefox or another browser
window. Right
click on http://webpages.cs.luc.edu/~anh/150/examples/Shortcut%20to%20idle.pyw.lnk
and save the link to the Python directory described in Step 1, above.
Task 1, Start IDLE
In Windows, go to your Python folder, and you should now see
the
shortcut you downloaded. Double click on the shortcut.
After this the instructions are the same in any operating
environment.
Python comes with a tool called IDLE. Idle can be
thought of
as a shell running an interpreter,
as an editor
or as an integrated
development environment (IDE.). Depending on the
configuration, Idle can start up showing either of two windows.
You could see the Python Shell, looking something like

or
it could look more like an Edit Window:

If
you see an Edit Window with its Run menu, go to the Run
menu and choose PYTHON SHELL
to open a shell. You may close the edit window for now if it is there.
Look at the Python Shell. The
>>> is the prompt, telling you IDLE is waiting for
you to type something.
Task 2, Using Python to do arithmetic
Try entering the following literal values at the prompt. (Hit
ENTER
after each)
-4
-4.0
3.5
3.14
0.80
Something odd should occur. Describe
it on paper.
Class discussion should eventually provide an explanation.
For now,
remember, Computers cannot precisely represent all decimal numbers.
Now try entering the following arithmetic expressions using
only a
single operator each. Note the response from the interpreter in
each case:
4 +
2
4 + 2.0 (note the difference from the
previous line)
3.5 – 4.0
5 * 7
23 / 4 (ignores the integer remainder)
23 % 4 (only the integer remainder)
23 / 4.0 (decimal division)
3 ** 2 (exponentiation: 3 to the second power)
9 ** 0.5 ( 9 to the 1/2 power or the square root of 9)
Task 3, Assigning values to variables, the print statement,
and the id function
To create a variable in Python we assign a value to the name. Python
does the rest.
= is the Python assignment operator. To
use the
value stored in a variable we simply use the variable in an expression.
Try the following, noting the output.
x = 5
y = x + 1
print y
print id(x), id(y)
x = 7
print x, id(x), y, id(y)
Use the above results to understand the
following points:
-
print is a way to tell the interpreter
to display one or more values, with multiple values separated by
commas.
-
= is assignment. Even if we read x=5
as "x equals 5", the meaning is "assign a memory location to the name x
and assign the value 5 to that memory location". This is not the same =
as in algebra; 5=x makes no sense at all in Python.
-
To use a value stored in a variable,
like 5 stored in x, just refer to x in an expression
-
id(x) invokes the built-in id function
on variable x. The function returns the memory location (address,
unique internal identifier) Python assigned to the name x. We will not
often use this function, but it will become
important to understand.
-
When we assign a new value to a
variable, e.g. x = 7, Python assigns a new
memory location to the
variable x. See that after the value of x is changed, its id
changes. (Fortunately Python takes care of reclaiming unused
memory
so we do not have to worry about it. This is called garbage
collection.)
Task 4, Python Identifiers
Recall (or look up) the rules for Python identifiers
on p. 29,
the
start of section 2.3.1. Figure out which of the following is
legal from the rules, and then test to see if you are
correct.
The expressions on the right side of the equals signs are all
legal. They define various sorts of data that we will discuss
as
we go on: integers, floating point numbers, strings, and
tuples
in parentheses.
mean = 43.5
my_score = 1
my_score = "hello there teddy bear"
the letter = "a test on one line"
LoyolaU = 6
C6H0= "the big game"
2hot = 'summer'
christmasday = (12, 25)
independence-day = (7, 4)
Try assigning a value to a reserved word from the top of p.
30.
Try entering
x = fred
Note the different form of the error message. The
error messages
further above for bad identifiers were syntax errors: errors
in
translation of the instruction. In this last case the syntax
was
legal, so the interpreter went on to execute the instruction.
Only then did it find the error described.
Call a TA or instructor to look at your work on the screen
and on paper.
Lab Directory
Course
Home Page