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?
x ** 2
yields the identical result that is produced by x * x
for all
integer and float values of x.x ** 2.0
yields the identical result that is produced by x * x
for all
integer and float values of x.print(5 + 6 % 7)
x = 3 % 4 + 1
y = 4 % 3 + 1
x, y = y, x
print(x, y)
x = 3
y = 4
print("x", "y", x + y)
5.5 - 11 / 2
5.5 - 11 // 2
10 % 7
7 % 10
3 + 2 * 2
16 / 4 / 2
x / y * z
is equal to x / (y * z)
.x / y ** z
is equal to x / (y ** z)
.w + x * y + z
is equal to (w + x) * (y + z)
.w % x + y % z
is equal to (w % x) + (y % z)
.m
and n
are ints, then m / n
and m // n
both evaluate to
ints.divmod
. Which values do x
and y
contain after:
x, y = divmod(13, 7)
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)?mm = ss // 60
mm = ss / 60
mm = ss % 60
mm = ss * 60
a = -10
b = 5
a < b or a < 0 and b < 0
(a < b or a < 0) and b < 0
x = (2 * 4 - 8 == 0)
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!
Student scores:
80.0
90.0
66.5
Average: 78.83333333333333
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)))
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)
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]))
In [ ]: