Description
1
1 Arithmetic expressions (15 points)
Convert each of the following mathematical expression in an equivalent Python
expression. Write the expression in the designated space in your template module a
line of Python code.
The values of the three rational variables a, b and c are assigned at the beginning
of the template module and should not be modified.
a. 4ab + bc
b. b
3
c. √
ab
d. (ac + ba)/bc
e. a
2 − b
4 + 2ab
2 Expressions, Python operators (5 points)
Write the following Python expressions into the designated places in the template
module. Run the module.
a. (8//6) ∗ 6
b. (8/6) ∗ 6
Explain in a comment line why the value of expressions a. and b. are different.
3 Boolean expressions (20 points)
Convert each of the following logical expression in an equivalent Python expression.
Write the expression in the designated space in your template module a line of
Python code.
The values of the Boolean variables a, b and c are assigned at the beginning of
the template module and should not be modified. Recall that ∨ is “or”, ∧ is “and”,
¬ is “not” and → is implication.
2
a. a ∧ (b ∨ c)
b. a → (b ∨ c)
c. (a ∧ b) ∨ (¬c)
d. a → (b → (¬c))
4 Function (30 points)
Wind chill is defined by the following formula:
Wind Chill = 13.13 + 0.621 × T − 12.1 × V
0.15 + 0.3967 × T × V
0.16
In this formula, T is the temperature in Celsius and V is the wind velocity
in kilometers per hour.
1) Define a function WindChill in your template module to calculate the wind chill
for any temperature and velocity. For instance, a call to WindChill(−20, 30) prints
the following result:
At -20C and 30 kph winds, it feels like -33.1156286189848C
Your function should NOT return anything, but PRINT the result on IDLE.
Call your windChill function with arguments -20 for T and 30 for V.
2) Write a similar function, named WindChill2, that does not print but returns the result. Write an appropriate statement using this second function, so as
to print this returned result.
3
5 Errors in a Python program (30 points)
def euc(x1,y1,x2,y1):
lambda = (x1-x2)**2
gamma = (y1-y2)**2
return((lambda+gamma)**(1//2))
result = euc(1,3,2,7)
print(“result”)
Function euc was written to calculate a distance with following formula:
p
((x1 − x2)
2 + (y1 − y2)
2
Unfortunately, the function definition, call and print statement contain 5 errors. Explain in a command line each error, within the template module. Re-write
correctly the function and the call/print statement in the template module.
6 Extra credit
This part is not mandatory but may provide extra credits, up to 5 points. Consider
the following Python function.
def myFct(value):
if(value==1):
return value
else:
return value+myFct(value-1)
Explain in a comment line the result returned by the function (2 points). You
should not paraphrase the code (this will not give you points), but rather explain
with one simple sentence what is the result of a call to this function.
4
B You may print the result of the function call with values 3, 4 and 5 ( i.e.,
successive calls), for instance. In addition, observe that the function is recursively
called by itself in the else statement. What is the effect of this instruction?
Write a simpler function myFct in the template module, that returns exactly the
same result, but encoded without using if and else and without the recursion call
myFct(value-1). Your function must NOT use more advanced concepts such as
while and for loops. (3 points).
5