CS 4010 Spring 2019

Lab 5

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 Lab5.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 4010
Lab 5
March 27, 2019


Find the Terminal - use the finder window and go to applications and then utilities.

Introduction to Python

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 Lab6.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).

Creating a Dictionary
We will start by writing the molecular weight program on page 156.
The first step is to create the dictionary of molecular weights:
Once you have the dictionary created, iterate through it with a for loop to compute the molecular weight of a given string.  You might want to experiment with some strings of your own.
You can follow the example at the top of page 157.
Cut and paste your program and the results of running it into your Lab5.txt file.

Now that you have a dictionary, lets experiment with some of the functionality available for dictionaries:
Fun with Lists
Here are some things we would like to do with lists:
Again, cut and paste you command lines and output, and any programs you write into Lab5.txt


Turn it in:
Go to the CS Homework Submission System: https://www.cs.csustan.edu/cshomework/
Choose: instructor Martin
              Spring 2019
              CS4010
              Lab5
Upload the your Lab5.txt file as prompted.