Monday, 08/05/2017: Review questions

As we could not cover in class all material that is necessary for the Homework and Project assignments, I give you here some review questions for the material covered.

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! If you are uncertain, then consult the Python help or the Internet.

  • Suppose the variable x has the value 5 and y has the value 10. After executing these statements:

    x = y
    y = x

    what will the values of x and y be, respectively?

    • True or False: x ** 2 yields the identical result that is produced by x * x for all integer and float values of x.
    • True or False: x ** 2.0 yields the identical result that is produced by x * x for all integer and float values of x.
  • What does Python print as a result of this statement?
    print(5 + 6 % 7)
  • What is the output from the print() statement in the following code?
    x = 3 % 4 + 1
    y = 4 % 3 + 1
    x, y = y, x
    print(x, y)
  • What is the output produced by the following code?
    x = 3
    y = 4
    print("x", "y", x + y)
  • For each of the following, determine the value to which the expression evaluates. (Your answer should distinguish between floats and ints by either the inclusion or exclusion of a decimal point.)
    • 5.5 - 11 / 2
    • 5.5 - 11 // 2
    • 10 % 7
    • 7 % 10
    • 3 + 2 * 2
    • 16 / 4 / 2
    • True or False: In general, x / y * z is equal to x / (y * z).
    • True or False: In general, x / y ** z is equal to x / (y ** z).
    • True or False: In general, w + x * y + z is equal to (w + x) * (y + z).
    • True or False: In general, w % x + y % z is equal to (w % x) + (y % z).
    • True or False: If both m and n are ints, then m / n and m // n both evaluate to ints.
  • Obtain help on the function divmod. Which values do x and y contain after:
    x, y = divmod(13, 7)
  • Assume the int variable ss represents a time in terms of seconds. Which of the following four possibilities is an appropriate statement to calculate the number of complete minutes in this time (and store the result as an int in the variable mm)?
    1. mm = ss // 60
    2. mm = ss / 60
    3. mm = ss % 60
    4. mm = ss * 60
  • What ist he value of the following boolean expressions:
    a = -10
    b = 5
    • a < b or a < 0 and b < 0
    • (a < b or a < 0) and b < 0
  • What is the value of x after the following code executes?
    x = (2 * 4 - 8 == 0)
  • To what value is c set by the following code?
    a = -3
    b = 5
    c = (a <= (b - 8))

Here some simple, small program exercises

  • Give Python-code to calculate the sine of $21^{\circ}$

    Hint: Consult the help to obtain information in which units the sine-function expects its argument!

  • Write a program that calculates the average score on an exam. Assume we have a small class of only three students. Assign each student’s score to variables called student1, student2, and student3 and then use these variables to find the average score. Assign the average to a variable called average. Print the student scores and the average score. The following demonstrates the program output when the students have been assigned scores of 80.0, 90.0, and 66.5:
Student scores:
80.0
90.0
66.5
Average: 78.83333333333333
  • Imagine that you teach three classes. These classes have 32, 45, and 51 students. You want to divide the students in these classes into groups with the same number of students in each group but you recognize that there may be some “left over” students. Assume that you would like there to be 5 groups in the first class (of 32 students), 7 groups in the second class (of 45 students), and 6 groups in the third class (of 51 students). Write a program that uses the divmod() function to calculate the number of students in each group (where each group has the same number of students). Print this number for each class and also print the number of students that will be “leftover” (i.e., the number of students short of a full group). Use simultaneous assignment to assign the number in each group and the “leftover” to variables. The following demonstrates the program’s output:
Number of students in each group:
Class 1: 6
Class 2: 6
Class 3: 8
Number of students leftover:
Class 1: 2
Class 2: 3
Class 3: 3

Exercise 1


In [3]:
import numpy as np
print(np.sin(np.deg2rad(21)))


0.358367949545

Exercise 2


In [12]:
import numpy as np
stu1 = 80.0
stu2 = 90.0
stu3 = 66.5
ave = (stu1 + stu2 + stu3)/3
print("Student scores:")
print(stu1)
print(stu2)
print(stu3)
print("Average: %f" %ave)


Student scores:
80.0
90.0
66.5
Average: 78.833333

Exercise 3


In [35]:
n1, lef1 = divmod(32,5)
n2, lef2 = divmod(45,7)
n3, lef3 = divmod(51,6)
n = [n1,n2,n3]
lef = [lef1,lef2,lef3]

In [42]:
print("Number of students in each group:")
for i in range(0,3):
    print('Class %d: %d' %(i+1,n[i]))
print("Number of students leftover:")
for i in range(0,3):
    print('Class %d: %d' %(i+1,lef[i]))


Number of students in each group:
Class 1: 6
Class 2: 6
Class 3: 8
Number of students leftover:
Class 1: 2
Class 2: 3
Class 3: 3

In [ ]: