If you need help reviewing Control Structures, take a look at these resources:
Each question has a "Toggle Solution" button -- click it to reveal that question's solution.
What would Python print?
Question 1
>>> x = 4
>>> x > 2 and x < 6
______True
>>> True and not True # a.k.a. a contradiciton
______False
>>> True and True # a.k.a. a tautology
______True
>>> False and True or True
______True
>>> False and (True or True)
______False
>>> False or True or 1 / 0
______True
>>> False and 1 / 0
______False
>>> 3 and 4
______4
>>> 3 or 4
______3
Question 2
>>> if True:
... print('True!')
... else:
... print('False!')
______True!
>>> if 4:
... print('True!')
... else:
... print('False!')
______True!
>>> if 0:
... print('True!')
... else:
... print('False!')
______False!
>>> x = 42
>>> if x < 0:
... print('negative')
... elif x == 42:
... print('The answer to everthing')
... else:
... print('Boring number')
______The answer to everything
Question 3
>>> x = 0
>>> while x < 5:
... x += 1
... print(x)
______1
2
3
4
5
>>> while False:
... print('hi!')
______# nothing happens
>>> while True:
... print('hi!')
... # press Control C to get out of this
______hi!
hi!
hi!
# forever
>>> def foo(n):
... while n > 0:
... if n * n == 8 * n - 16:
... return True
... n -= 1
... return False
>>> foo(3)
______False
>>> foo(9)
______True
Environment Diagrams
Question 4
def branch(x):
if x > 10:
x -= 5
elif x > 7:
x -= 2
if x % 2 == 0:
return 'even'
else:
return 'odd'
a = branch(12)
b = branch(8)
Question 5
def uhoh(x):
if x:
y = 5
return y
a = uhoh(True)
b = uhoh(False)
Question 6
def is_even(x):
return x % 2 == 0
i = 0
while i < 2:
if is_even(i):
print(i)
i += 1
Code-Writing question
Question 7
For each of the following functions, try to make the code more concise.
def one(x):
if x == True:
return 'input is true'
else:
return 'input is false'
def two(x):
if x == 100:
return True
else:
return False
def three(x):
if x % 6 == 0:
x += x // 6
return x
else:
return x
def four(ones_win):
if ones_win == True:
result = 6
elif ones_win == False:
result = 4
def one(x):
if x:
return 'input is true'
return 'input is false'
def two(x):
return x == 100
def three(x):
if x % 6 == 0:
x += x // 6
return x
def four(ones_win):
result = 6 if ones_win else 4
Question 8
Write a function summation
that adds the first n
elements in a
sequence. The k
th element in the sequence can be computed by
evaluating term(k)
.
def summation(n, term):
"""Computes the summation of the first n numbers in the
sequence defined by the function term.
>>> square = lambda x: x * x
>>> summation(5, square)
55
"""
"*** YOUR CODE HERE ***"
def summation(n, term):
k, total = 1, 0
while k <= n:
total += term(k)
k += 1
return total
Question 9
Write a function is_fib
that returns True
if its input is a
fibonacci number, and False otherwise.
def is_fib(n):
"""Returns True if n is a fibonacci number,
else False
>>> is_fib(8)
True
>>> is_fib(9)
False
"""
"*** YOUR CODE HERE ***"
def is_fib(n):
cur, next = 0, 1
while cur < n:
cur, next = next, cur + next
return cur == n