Monday, 15/05/2017: Review questions

Here some review questions and (very simple) exercises for the material that we covered in class. They should help you to identify topics that you should further reivew with the literature or the Internet.

Please feel free to e-mail Joseph or myself for any questions you might have!

Please try to answer the following questions before executing any code. Do this only to verify your answers!

  • What is the output of the following code:
    i = 1
    while i <= 10:
        j = 1
        while j<= 10:
            print(i * j, end=" ")
            j = j + 1
        print()  # This just prints a newline    
        i = i + 1
  • What is the output of the following code:

    i = 1
    while i > 0:
        print(i**2)
        i = i + 1

    Hint: If you want to run this code, do it preferably as a script! It might crush a notebook session.

  • What is the output of the following code:

    day = "Saturday"
    
    if day == "Saturday":
        print "We go to the Rhine"
        print "We will get some ice cream"
    
    if day == "Sunday":
        print "We stay at home"
    print "We meet Lucie and Mike in the afternoon"
  • What is the output of the following code:

    a = 5
    b = 10
    if (a < b) or ((a < 0) and (b < 0)):
        print("Yes, it’s true.")
    else:
        print("No, it’s false.")
    
    if ((a < b) or (a < 0)) and (b < 0):
        print("Yes, it’s true.")
    else:
        print("No, it’s false.")
  • You get the following code which should print out the smallest of three integers $x$, $y$ and $z$:

    x = 15 
    y = 5 
    z = 11 
    
    print("Your numbers: ", x, y, z) 
    if x < y: 
        if x < z: 
            print("x is smallest number")
        else: 
            print("z is smallest number")
    else: 
        print("y is smallest number")

    Is the code correct for all possibile numbers $x$, $y$ and $z$? If not, then give a correct version of the code.

  • What is the output of the following code:

    def f(x):
        return x + 2, x * 2
    
    x, y = f(5)
    print(x + y)
  • What is the output of the following code:

    def calc_q1(x):
        q = 4 * x + 1
        return q
    
    calc_q1(5)
    print(q)
  • What is the output of the following code:

    def calc_q4(x):
        q = 4 * x + 1
    
    print(calc_q4(5))
  • What is the output of the following code:

    def inc_by_two(x):
        x = x + 2
        return x
    
    x = 10
    inc_by_two(x)
    print("x = ", x)
  • What is the output of the follwing code:

    def f1(x, y):
        return (x + 1) / (y - 1)
    
    def f2(x, y=2):
        return (x + 1) / (y - 1)
    
    print(f1(3, 3))
    print(f2(3), f2(3, y=4))
  • Write a function convert_temp which takes a temperature $T_f$ in Fahrenheit (float-number) as input. It should return the temperature in celsius $T_c$ and in Kelvin $T_K$. The conversions are given by:
    • $T_c = \frac{5}{9}(T_f-32)$
    • $T_K = T_c + 273.15$
  • You probably did the core parts of this exercise in class already. The task is to turn your existing code into a function.

    The square root $y=\sqrt{x}$ of a positive number $x$ can be estimated iteratively with $y_0>0$ (an arbitrary positive number) and $y_{n+1}=\frac 12\left(y_n+\frac{x}{y_n}\right)$. The procedure can be implmented with a while-loop testing for the condition $|y_{n+1}-y_n|>\epsilon$. $\epsilon$ represents the accuracy of the final square-root estimate. You update $y_n$ and $y_{n+1}$ within the loop and consider the final $y_{n+1}$ as estimate for $\sqrt{x}$.

    Write a python-function my_sqrt to estimate the square root with that recipe. The function should accept as arguments the number from which to estimate the square root and the required accuracy $\epsilon$. $\epsilon$ should be an optional parameter with a default value of $\epsilon=10^{-6}$.

    • If the user provides an invalid input (a number smaller than zero), the function should print an error message and return None.
    • If the user input is valid, the function should return the square root estimate.

In [ ]: