Activity 2-1

October 6, 2015

Task 1: Python Expressions and Assignments

In this task, you will get more practice writing, evaluating, and assigning expressions to variables in Python. In particular, we will explore lists and strings. Open IDLE and interact with the terminal to complete the following steps:

  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 (using the + operator) you wrote in the previous step together. What do you get? Why?

Task 2: Python Functions

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

def addOne(t):
  y = t + 1
  return y

z = addOne(x)

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?