Activity 2-3

March 5, 2013

Task 1: Understand and Run Last Class's Programs

Download and save ACT2-3.py. Open it in IDLE.

  1. Look at readMobyDick(). Make sure you understand the following line:
    fileString = fileString[0:1000]
  2. Look at CountWordsInMobyDick(). Make sure you understand the following lines:
    for word in myList:
      count = count + 1
            
  3. Press F5 and run countWordsInMobyDick(). What's your answer?
  4. Comment out two lines in readMobyDick() to count all the words in MobyDick (hint: one of the lines is a print statement, which technically has no bearing on how the rest of the function executes). Press F5 again and run countWordsInMobyDick() again. What's your answer?

Task 2: More Python Practice

Do this activity with a partner. You don't have to turn anything in.

  1. With a pen or pencil, write what you expect to see for each expression below. Assignments will return nothing, so don't worry about those.
  2. Enter the statements below and verify your answers. The line numbers on the left-hand side are for reference only.
    myString = 'hi there!'
    len(myString)
    myList = myString.split()
    len(myList[1])
    
    x = 10
    y = 15
    str(x) + str(y)
    [x]+[y]
    
    range(1,11)
    range(3,10)[0] #this is tricky
    
    negNums = range(-7,1)
    negNums[3:5]
    
    (x == 10) and (str(y) == 'fifteen')
    (x < 4) or (y*0.1 < 4)
    
    if len(myList[1]) > 10:
        print myList[1],'has length > 10'
    else:
        print myList[1],'has length <= 10'
    
    def tip(bill):
        return bill * 0.15
    
    tip(100.00)