EMS412 Python Practice Test Solutions
EMS412 Python Practice Test Solutions
Q1: complete the function that will return the multipication of 3 given numbers - a,b,c.
1 def multiply_numbers(a,b,c):
2 """insert the code that performs the operation.
3 Use the variable 'number' to hold and return the value """
4
5 ### BEGIN SOLUTION
6
7 number = a*b*c
8 ### END SOLUTION
9 return number
10
Q2: complete the function that returns the area of a circle with radius r , note, use math.pi for pi.
In [19]: ID: circle_area Autograded answer
1 import math
2 def circle_area(r):
3 """calculate the area given radius r,
4 use the variable named 'area' to hold and return the correct value """
5 ### BEGIN SOLUTION
6 area = math.pi*r**2
7 ### END SOLUTION
8 return area
9
sector of a circle with radius r and angle theta (in degrees). Note, use math.pi for
Q3: complete the function that returns the area of a segment
pi.
In [21]: sector
ID: area_segment Autograded answer
1 import math
2 sector
def segment_area(r,theta):
3 sector’s area.
"""calculate the area given radius r and angle theta, return the segment's
4 Use the variable named 'area' to hold and return the correct value """
5 ### BEGIN SOLUTION
6 area = (theta/360.)*math.pi*r**2
7 ### END SOLUTION
8 return area
9 sector’s
10
𝑒
Q4: complete the function that calculates the Euler's number from the series you learnt during the maths lessons. The funcion takes in
the value n, this will be the term that you truncate the series at. Also use the following hints.
Hint 1: remember the Maclaurin Expansion 𝑒𝑥 = 1 + 𝑥 + 𝑥2!2 + 𝑥3!3 +...... 𝑥𝑖!𝑖 +....
Hint 2: you will need to use a for loop,
1 import math
2 def calculate_e(n):
3 """ Here use the variable e to denote and return the value of e.
4 I have initiated the value of e (e=0) and defined the point where you will evaluate the series - that is
5 The value of variable n denotes the number of terms to use in the series.
6 So write the loop to compute the first n terms. """
7
8 e = 0.
9 x = 1
10
11 ### BEGIN SOLUTION
12
13 for i in range(n+1):
14 e = e + x**i/math.factorial(i)
15
16 ### END SOLUTION
17 return e
18
19 print(calculate_e(8))
2.71827876984127
Q5: Complete the function that calculates the position of an object at any time t≥ 0. The initial position and the initial velocity (at time t=0)
are denoted as s0 and v0 respectively. The object undergoes constant acceleration a, for any time t>0.
1 def calculate_position(s0,u0,a,t):
2 """given the initial conditions and acceleration, return the object's position.
3 Use the variable named 'position' to hold and return this value """
4 ### BEGIN SOLUTION
5
6 position = s0 + t*u0 + 0.5*a*t**2
7 ### END SOLUTION
8 return position
9
10
11
12
𝑥
Q6: Complete the function that takes in a number , and returns the value 1 if 𝑥 ≥ 0, otherwise it is 0 - this is called a step function
In [27]: ID: cell-707062d257fe4a7f Autograded answer
1 def step_function(x):
2 """given x compute the correct value to return.
3 Note you need to include the return statement here"""
4
5 ### BEGIN SOLUTION
6 value = 0
7 if x >= 0 :
8 value = 1
9 return value
10 ### END SOLUTION
11