Problem Session 1 Solutions

  1. [PP Problem 2.1] Indicate whether the result is an int or float by (not) including a decimal point, e.g. 5 for an int, 5.0 for a float.

    For each of the following expressions, what value will the expression give?

    a. 6
    b. 20.0
    c. 4.5
    d. -4.5
    e. -5
    f. 1
    g. 1.0
    h. 1.0
    k. -4.5
    l. 19
    m. 35

  2. Write a function named average that has two numbers as parameters and returns the average of those values.

     def average(a, b):
         """
         Return the average of a and b
         """
         return (a + b) / 2
    
  3. Given variables x and y, which are assigned the values 3 and 12.5, respectively, what will the following expression print?

    a. The rabbit is 3.
    b. The rabbit is 3 years old.
    c. 12.5 is average.
    d. 12.5 * 3
    e. This will generate a Type Error as you can’t perform + on strings and numbers. The operands either need to both be numeric types or both be strings.