Lab 2
Using Booleans and Lists
Due within an hour after lab

Overview

This lab practices Boolean expressions, conditional statements, and working with Lists. Simple programs are created using them.

Interactive Boolean Expressions

Using the interactive Python shell, type in and evaluate Python expressions for each of the following. Use online resources if you are not sure of the needed Python operator for performing an operation.

  1. First make the following assignments in the interactive shell:
          y = 23
          word = "fuzzy"
          flag = True
          catalog = ["desk", "chair", "table", "lamp"]
        

    Try out the following expressions. Make sure you predict the outcome before you hit Enter!

    1. y < 10
    2. y == 23
    3. y != 23
    4. y < 10 or y > 20
    5. word == "fuzzy"
    6. y > 20 and word == "smooth"
    7. "desk" in catalog
    8. not ("desk" in catalog)
    9. word in catalog or flag
  2. Write a Boolean expression that evaluates to true if the variable x is (inclusively) between 10 and 20. Your expression should consist of two complete Boolean expressions joined by the and key word.

Using lists

Make the following assignments:

     zoo = ["asp", "emu", "owl", "zebra"]
     zoo2 = zoo
     farm = ["cat", "cow", "pig"]
   

Also run this statement so that random functions can be called:

      import random
    

Write Python expressions or statements that do the following (refer to Table 2.3 or type help(list) for help):

  1. Produces the number of strings in zoo
  2. Creates a new list called herd that consists of all 7 strings from zoo and farm.
  3. Inserts the string "bat" before "cow" in the farm list.
  4. Make a copy of the farm list and call it farm2. Then remove "pig" from farm2. Check if farm is affected.
  5. Remove "owl" from zoo. Check if zoo2 is affected.
  6. Try out this expression: random.choice(zoo). What does it do? Does it always produce the same result?

Python Short Programs

  1. Write and test a Python program that reads in (using the input function) a person's name (e.g. "Sally") and writes out (using the print function) a random goodnight wish to that person. For example, sometimes the output might say "Night night Sally!" and sometimes "Sweet dreams Sally!" The random goodnight wish should be randomly chosen from a list of strings that you create (see exercise above).
  2. (optional) Write a Python program that inputs an amount in dollars and inputs the destination state (e.g. IL, OH, WI, etc). It should report the total costs according to the following rules:
    • If the shipping is within state (i.e. destination state is IL), add 6.25% to the total (before shipping cost). If out of state (anything else), don't add a tax.
    • If the amount is less than $10, the cost of shipping is $1.75. Otherwise, shipping is free.

Deliverable

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

  1. A statement that summarizes your completion of the assignment. It should include how you collaborated and who you worked with (if anyone).
  2. The text of your interactive Python exercises
  3. For each of your Python scripts:
    1. Listings of your python programs (just paste them in the txt file)
    2. Running examples that demonstrate that your python programs work correctly

Submit your lab2.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.