Control Structures: basic-level questions

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 and not True    # a.k.a. a contradiciton
______
>>> True and True        # a.k.a. a tautology
______
>>> False and True or True
______
>>> False and (True or True)
______
>>> False or True or 1 / 0
______
>>> False and 1 / 0
______
>>> 3 and 4
______
>>> 3 or 4
______

Question 2

>>> if True:
...     print('True!')
... else:
...     print('False!')
______
>>> if 4:
...     print('True!')
... else:
...     print('False!')
______
>>> if 0:
...     print('True!')
... else:
...     print('False!')
______
>>> x = 42
>>> if x < 0:
...     print('negative')
... elif x == 42:
...     print('The answer to everthing')
... else:
...     print('Boring number')
______

Question 3

>>> x = 0
>>> while x < 5:
...     x += 1
...     print(x)
______
>>> while False:
...     print('hi!')
______
>>> while True:
...     print('hi!')
...     # press Control C to get out of this
______
>>> def foo(n):
...     while n > 0:
...         if n * n == 8 * n - 16:
...             return True
...         n -= 1
...     return False
>>> foo(3)
______
>>> foo(9)
______

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 kth 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