CS 4950 Spring 2016
Lab 4
Find a computer and log into the
OSX side using your CS login name and password.
Open Firefox and find the class web
page.
Open TextWrangler - use the finder
window and go to applications.
Open a new text file, called Lab3.txt, to record your work today -
you will paste in some of your command lines. The header of the file
should look like:
Your Name
CS 4950
Lab 4
March 11, 2016
Find the Terminal - use the finder
window and go to applications and then utilities.
We are going to start out using the Python
interpreter
In the terminal type Python to open then interpreter, then at the
prompt type the following and enter
>>> print "hello world"
For each command you run, copy the command line and the output into
your Lab4.txt file.
Create a list and a sum variable
>>>
L = [2,3,4,5,6]
>>> sum = 0
Now add all of the elements in the list (you will need to hit enter
to get back to the prompt after sum = sum + i)
>>>
for i in L:
...
sum = sum + i
...
>>> print
sum
Use the built-in function len() to get the length - number of
elements in the list L
>>>
len(L)
Use a simple if statement to check for an element in L
>>>
if 3 in L:
...
print 'found it'
...
Now try out the arithmetic operators to do some computations and
make some notes about what you observe
>>>
16*2+5/4
>>>
(16*2+5)/4
>>>
(16*2+5)/4.0
>>> 3**2
>>> pow(3,2)
>>> 3%2
>>> 5%4
>>> 6%4
>>> sqrt(2)
>>> import
math
>>> print
"math.sqrt(100) : ", math.sqrt(100)
Now work through the examples starting on page 128 in the
interpreter (not as a script yet).
You can exit the interpreter by typing ^d (control d).
If you get to the end, put it in a script and try to run it.
Even if it is not in your script directory you can always run it
from the directory where it is saved by typing:
python
myscript.py
where myscript.py is the name of your file.
Turn it in:
Go to the CS Homework Submission System: https://www.cs.csustan.edu/cshomework/
Choose: instructor Martin
Spring 2016
CS4950
Lab4
Upload the your Lab4.txt file as prompted.