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:
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

Python Shell

or it could look more like an Edit Window:

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:

  1. print is a way to tell the interpreter to display one or more values, with multiple values separated by commas.
  2.  = 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.
  3.  To use a value stored in a variable, like 5 stored in x, just refer to x in an expression
  4.  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.
  5. 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