Quiz 2 Study Guide

Quiz 2 will be in class on Friday, October 6 at the start of class.

Python concepts and functions

You will tested material needed for Labs 1 through 3, which includes for loops, if statements, bool types, relational operators, and logical operators.

This quiz will not include while loops or functions.

Study guides are not graded. You may work with other students on the questions, and ask questions about the guide during evening ninja help sessions, on EdSTEM, or with meetings with faculty and staff. We do not provide full solutions to the study guides.

In addition to all concepts from Quiz 1, you should understand the following:

  • using range with start, stop, and step parameters

  • for loops that iterate over items in a sequence using for <item> in <sequence>

  • for loops that iterate over the indexes of a sequence using for <index> in range(len(<sequence>))

  • the accumulator pattern, including accumulating integers and strings

  • indexing a string or list

  • the bool data type representing True and False

  • if, if/else, if/elif/else

  • the relational operators <, <=, ==, >, >=, !=

  • how strings are ordered when compared using relational operators.

  • the logical operators and, or, and not

  • nested statements (e.g., using an if/else in a loop)

Sample practice problems

  1. What sequence of numbers do each of the following range statements generate:

    range(4)
    range(0, 4)
    range(7, 9)
    range(10, 0)
    range(2, 10, 2)
    range(2, 11, 2)
    range(10, 0, -1)
  2. Given these assignments:

    x = 5
    y = 13
    phrase = "computer science"
    name = "Rich"
    b = False

    Show the value and type of each expression below:

    (x < 10) and (y < 10)
    (x < 10) or (y < 10)
    (x < 10) and (x > 0)
    (x > 10) or (x < 0)
    (5/x) > 7.0
    not b
    b or not b
    len(phrase) >= 10
    phrase[0] < phrase[1]
    name < phrase
  3. The code below asks the user for two input values. Provide 5 different sets of input values for first and second such that each branch of the if statement is followed.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    def main():
        first = int(input("Enter the first number: "))
        second = int(input("Enter the second number: "))
        if first < 10:
            print("Option 1")
        elif second < 10:
            print("Option 2")
        elif first < second:
            print("Option 3")
        elif second > 20:
            print("Option 4")
        else:
            print("Option 5")
    
    main()
    
  4. Show the output from the following code fragments:

    word = 'october'
    result = ''
    for letter in word:
        result = result + letter
    print(result)
    word = 'october'
    result = ''
    for i in range(len(word)):
        result = result + word[i]
    print(result)
    1. Modify the second block of code so that it prints otbr

    2. Modify the second block of code so that it prints coe

    3. Modify the second block of code so that it prints rebotco

  5. Write a program that asks the user to enter a word and a letter. Your program will then count how many times that letter appeared in the word. For example:

    Enter a word: mississippi
    Enter a letter: s
    The letter s appears 4 times in mississippi
  6. Write a program that asks the user to enter the hour of the day and whether it is AM or PM. You can assume the user always provides an integer between 1-12 for the hour and that they always type either AM or PM. Based on the time of day, tell them if Narples is open and what meal they are serving. For this question, assume that Narples serves breakfast from 7 AM to 10 AM, lunch from 11 AM to 2 PM, dinner from 4 PM to 9 PM, and is closed every other time. For example:

    What is the hour? 8
    Is that AM or PM? AM
    Narples is serving breakfast.
    
    What is the hour? 2
    Is that AM or PM? PM
    Narples is serving lunch.
    
    What is the hour? 9
    Is that AM or PM? PM
    Narples is serving dinner.
    
    What is the hour? 11
    Is that AM or PM? AM
    Narples is serving lunch.
    
    What is the hour? 11
    Is that AM or PM? PM
    Narples is closed.