# Activity 2-1

February 26, 2013

## Task 1: Python Expressions and Assignments

1. Write an expression that averages the numbers 2, 5, and 9. Make sure you get a decimal number (a `float`).
2. Assign the list of numbers `[2,5,9]` to a variable called `myList`. Write an expression that averages the elements of `myList`.
3. Assign the string `"2 5 9"` to a variable called `myString`. Using indexing, write an expression that just returns `"2"`. Do the same for `"5"` and `"9"`.
4. Add the three expressions you wrote in the previous step together. What do you get? Why?

Consider the following Python program. The numbers to the left of the green line are just line references, and not part of the program; you wouldn't type them if you were writing this program yourself.

```y = 10
x = y + 5

y = t + 1
return y

z
y
```

Starting at Line 1, go through the program and write down the variable table (like we just did in class). What does `z` output in Line 9? What does `y` output in Line 10?

## Task 3: Splitting a String and Working with Files

1. The `split` function, without an input argument, splits a string on any whitespace (including spaces, newlines, tabs, etc.). Use the expression `fileString.split(<delimiter>)` to split on different delimiters by passing a string delimiter as input to the `split` function. Observe what happens when the delimiter is:
• `"\n"`
• `"a"`
• `"Sarah"`
2. In `ACT2-1.py`, write a function called `readShel` that opens the poem and returns a list of words. We have already written each line in class already — you just need to put them together.

## Task 4: Counting the Number of Words in Moby Dick

Download and save `MobyDick.txt`. In `ACT2-1.py`, write a `readMobyDick()` function and a `countWordsInMobyDick()` function. Note that only a few things change from the `Shel` functions.

Be sure to save this python file! It will be useful for the next class.