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:
- find the file aminoacid_table.html
in the books examples using Finder
- drag the file to Firefox
- once it is open, use Command-u to get the source code
- copy the source code you will need to create the dictionary
into TextWrangler
- now write a regular expression to capture the letter and
weight data (see bottom of page 156)
- turn it into a dictionary be adding the name = { at the beginning and } at the end
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:
- Use the .get()
function to retrieve the molecular weight for 'E'
- Try printing the dictionary - what do you notice about the
result?
- Now try printing the keys and values separately
- Next print the sorted key and compare to the keys you printed
above
- Modify the
dnacalc.py program form earlier, using the code in the
box in the middle of page 158
- notice the percent calculation done in a single line
- notice the formatted printing
Fun with Lists
Here are some things we would like to do with lists:
- create a list from a string
- create a list from the string "FDILSATFTYGNR"
- define an empty list
- define a list with elements in it
- define a new list with the elements cat, dog, bird
- define numerical lists with the range function
- use the range function to define a list containing 1,2,3,4,5,6,7,8,9,10
- can we use the range function to create the same list but in
descending order?
- parse strings into lists with .split()
- figure out how to use split to turn the string "to be or not to be"
into a list of words
- added elements to lists with .append()
- use the list you
created with cat, dog, bird, append snake and rat
- insert elements to a specific place in a list
- create a list with 1,2,3,4,5 then inert the elements 9,8,7
in between 2 and 3
- delete elements for a list with del
- delete elements 7 and
3 from the list above using indexes and del
- delete indexed elements in a list with assignment to []
- delete elements 7 and
3 from the list above using assignment
- use the .join() function to create a
string from a list
- you create a list of
your choice and then turn it into a string
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.