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.

NoteHint

Remember to define the radius and use the formula: circum_circle = 2 * pi * radius.

TipFully worked solution
radius = 5
circum_circle = 2 * pi * radius

1B) Calculate the area of a circle with radius 2.5 and assign to area_circle.

NoteHint

Use Area = pi * radius**2.

TipFully worked solution
area_circle = pi * radius**2

Question 2

2A) Access the second name in the names list and assign to chemist.

NoteHint

Second element has index 1.

TipFully worked solution
chemist = names[1]

2B) Add "Albert Einstein" to the names list.

NoteHint

Use names.append("Albert Einstein").

TipFully worked solution
names.append("Albert Einstein")

Question 3

3A) Access the value for quantity = "temperature" in quantity_units and assign to units.

NoteHint

Access dictionary values with dictionary[key].

TipFully worked solution
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 print function to check the value within your quantity_present variable (boolean). If this produces a NameError, you may need to check that quantity_present has 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).
NoteHint

Use key in dictionary to check membership.

TipFully worked solution
if second_quantity in quantity_units:
    quantity_present = True
else:
    quantity_present = False
# OR
quantity_present = second_quantity in quantity_units

Question 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\]

NoteHint

Convert each height to metres and subtract: (h2 * conversion - h1 * conversion).

TipFully worked solution
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_m is greater than 0.5 metres (50cm), create a variable called check and set this to 1
  • If height_difference_m is between 0.3 and 0.5 metres (30 to 50cm), create a variable called check and set this to 2
  • If height_difference_m is something else, create a variable called check and set this to 3

do this by constructing and ifelse block.

NoteHint

You need an if, elif else construct.

TipFully worked solution
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