Lab 4
Lists and Counters
Due within an hour after lab/class time

Overview

This lab practices working with lists, counters and strings.

Loading lab4.py

If you haven't yet, load the l4.py file and run its module. Note that the assignment statements and defining the functions do not produce any additional output.

Exercises

As instructed, either provide the written answer, Python expression or Python statements. Most of these exercises call for using the interactive shell.

  1. Study the first two assignment statements and explain how the variable words gets its value.
  2. Using the len function, produce an expression that indicates how many strings are in the words list.
  3. Using the len function, produce an expression that indicates how many letters are in the first word in words list (it should evaluate to 3).
  4. Study the function show_vowel_words. Can you predict what happens if you type show_vowel_words(words) into the interactive shell? Provide a couple of sentences that explain what happens.
  5. Copy the code for show_vowel_words and rename the copy show_long_words. Now modify it so that show_long_words prints words in the parameter's list that are longer than 5 letters. For example, show_long_words(words) should print aardvark and elephant.
  6. Create another function called show_numbers whose parameter is a list of numbers and print every number in the list. For example, calling show_numbers(lucky_numbers) should print all 6 numbers. Then, modify show_numbers so that it only prints positive (greater than 0) numbers.
  7. Study the doubleStats function and then run it giving 100 as the argument. Does it return a value that you would expect? Explain what doublesStats counts.
  8. Write a Python expression that evaluates to an estimate of the probability of rolling doubles (hint: take the number of doubles and divide by the number of trials; be sure to use a large number of trials to get a good approximation). Be sure to clearly label your code and answer. Make sure you use a good sample size (number of trials).

Deliverable

Create a text file called lab4.txt that contains the following:

  1. A statement that summarizes your completion of the assignment, including any collaboration.
  2. Answers to the interactive questions.
  3. For each of your Python functions and test code:
    1. Listings of your functions and test code (just paste them in the txt file)
    2. Running examples that demonstrate that your python functions work correctly

Submit your lab4.txt file to D2L.

Grading

Your lab submission will be graded using the following rubric:

  • + .5 --- Your submission is clearly formatted.
  • + .5 --- Your submission includes a summary statement and includes how you collaborated.
  • + .5 / 1.0 --- You submitted most of the lab (0.5) or you submitted all of the lab (1.0).
  • + .5 --- Your lab submission is generally correct.

Labs submitted late are subject to a half point penalty.