Variables, lists, dictionaries and branches
This is a formative test. It is an occasion to practice the course material. It does not contribute to your final grade.
Using the topics covered within the workshops (or otherwise), complete the questions below.
Make sure to use any described variable names exactly and do not change the name of this file. This ensures the nbgrader tool can grade your work correctly.
Question 1
1A) Using the pi variable defined below (representing \(\pi\) to 6 decimal places), calculate the circumference of a circle with radius of 5 and assign to a variable called circum_circle.
Remember to define the radius and use the formula: circum_circle = 2 * pi * radius.
radius = 5
circum_circle = 2 * pi * radius1B) Calculate the area of a circle with radius 2.5 and assign to area_circle.
Use Area = pi * radius**2.
area_circle = pi * radius**2Question 2
2A) Access the second name in the names list and assign to chemist.
Second element has index 1.
chemist = names[1]2B) Add "Albert Einstein" to the names list.
Use names.append("Albert Einstein").
names.append("Albert Einstein")Question 3
3A) Access the value for quantity = "temperature" in quantity_units and assign to units.
Access dictionary values with dictionary[key].
units = quantity_units[quantity]3B) For the variable second_quantity defined below, check whether this key is present in the quantity_units dictionary. If this is in the dictionary create a variable called quantity_present and assign this to a value of True, otherwise assign this to a value of False.
Notes:
- You can use the
printfunction to check the value within yourquantity_presentvariable (boolean). If this produces aNameError, you may need to check thatquantity_presenthas been successfully created. - Check your Week 3 notes for examples of how to check membership (i.e. whether a value is contained within a collection like a list or a dictionary).
Use key in dictionary to check membership.
if second_quantity in quantity_units:
quantity_present = True
else:
quantity_present = False
# OR
quantity_present = second_quantity in quantity_unitsQuestion 4
4A) Two values of heights in units of feet are provided below in a list called height_feet. Convert these values to metres and calculate the difference in metres. Store this difference in a variable called height_difference_m.
Conversion from feet to metres can be done using the equation:
The conversion equation is: \[ \mathrm{height_{feet}} = \mathrm{height_{m}} \times 3.28084\]
Convert each height to metres and subtract: (h2 * conversion - h1 * conversion).
height_metre = [height_feet[0]/feet_per_metre, height_feet[1]/feet_per_metre]
# or if you have learnt about loops and list comprehension
height_metre = [h/feet_per_metre for h in height_feet]
height_difference_m = height_metre[1]-height_metre[0]4B) Check whether height_difference_m is:
- If
height_difference_mis greater than 0.5 metres (50cm), create a variable calledcheckand set this to1 - If
height_difference_mis between 0.3 and 0.5 metres (30 to 50cm), create a variable calledcheckand set this to2 - If
height_difference_mis something else, create a variable calledcheckand set this to3
do this by constructing and if–else block.
You need an if, elif else construct.
check = 0
if height_difference_m > 0.5:
print(f"{height_difference_m:.3f} is greater than 0.5m")
check=1
elif height_difference_m > 0.3 and height_difference_m <= 0.5:
print(f"{height_difference_m:.3f} is greater than 0.3m but less than or equal to 0.5m")
check=2
else:
print(height_difference_m)
check=3