Quiz 2 Study Guide

Quiz 2 will be in class on Friday, February 25 at the start of class.

Python concepts and functions

You are responsible for all material needed for Labs 1 through 4 which includes `for` loops, `if` statements, Boolean types, relational operators, and logical operators, `while` loops, and functions.

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:

• the Boolean data type

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

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

• the logical Boolean operators `and`, `or`, and `not`

• nested statements (e.g., nested `if` statements, `if`/`else` in a loop, etc.)

• definite `for` loop vs indefinite `while` loop

• function definitions and function calls

• function parameters and function arguments

• string operations

Sample practice problems

1. Given these assignments:

``````x = 5
y = 13
S = "we love computer science"
Done = 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
len(S) >= 10
S[0] < S[1]
not Done``````
2. The code fragment below asks the user for two input values. Provide 5 different sets of input values for `number_one` and `number_two` such that each branch of the if statement is followed.

``````number_one = int(input("Enter the first number: "))
number_two = int(input("Enter the second number: "))
if number_one < 10:
print("Option 1")
elif number_two < 10:
print("Option 2")
elif number_one < number_two:
print("Option 3")
elif number_two > 20:
print("Option 4")
else:
print("Option 5")``````
3. Show the output from the following code fragments:

``````x = 0
while x < 6:
print("%d  %d" % (x, x**2))
x = x + 1
print("done!")``````
``````text = "strings!"
i = 0
found = False
if text[i] in 'aeiou':
found = True
else:
print(text[i])
i = i + 1
print("found at position %d" % (i) )``````
4. Consider the following program. Note that calling `int` on a float argument always rounds down to the nearest integer. For example, `int(7.9) == 7`.

``````x = 3
i = 0
print("%d  %d" % (0, x))
while x != 1:
if (x % 2) == 0:
x = int(x / 2)
else:
x = int(( 3*x + 1 ) / 2)
print("%d  %d" % (i, x))
i = i + 1``````
1. Trace the program and show its output.

2. Create a table having columns for `i`, and `x` and show how the values change as the loop executes.

5. Given the program below:

``````1  def find_min(a, b):
2      min_value = a
3      if b < a:
4          min_value = b
5      return min_value
6
7  def main():
8      number_one = int(input("Enter the first number: "))
9      number_two = int(input("Enter the second number: "))
10     smaller = find_min(number_one, number_two)
11     print("The smaller value is %d" % (smaller))
12
13 main()``````
1. What are the function definitions?

2. What are the function calls for functions defined within this program?

3. Which functions are called that aren’t defined within this program?

4. Give an example of a parameter and give an example of an argument.

5. What would this program print if the user entered the following two numbers:

• 4 and 5

• 5 and 5

• 9 and 2

6. Consider the following program:

`````` 1  def mystery(word, letter):
2      print("word: %s" % (word))
3      print("letter: %s" % (letter))
4      result = ""
5      for ch in word:
6          if ch != letter:
7              result = result + ch
8
9      # draw stack here
10      return result
11
12  def main():
13      test = "hello"
14      check = "l"
4. What does the `mystery` function do?
5. What would `mystery("banana", "a")` return?
6. What would `mystery("zoo", "a")` return?