Assignment 3: Core Python#
At-home assignment — worth 10 points. When you’re done, push your work to your week folder and post a link to your completed notebook on the matching Courseworks assignment.
Learning goals
This assignment exercises the core Python skills from this section:
Create and index lists and dictionaries
Iterate through lists, tuples, and dictionaries
Define functions with positional and keyword arguments
Working through this notebook
Download this notebook using the ⬇ icon in the top-right, or copy-paste each task into a fresh notebook in your environment (JupyterLab on LEAP or Colab). Fill in the empty code cell under each numbered task with your solution.
When you’re done, follow the submission instructions at the bottom of the page.
Part I: Lists and Loops#
In this part you’ll exercise lists and flow control by manually creating data structures.
Warning
Pluto is not a planet anymore.
1) Create a list with the names of every planet in the solar system (in order)#
2) Have Python tell you how many planets there are by examining your list#
3) Use slicing to display the first four planets (the rocky planets)#
4) Iterate through your planets and print the planet name only if it has an s at the end#
Part II: Dictionaries#
1) Now create a dictionary that maps each planet name to its mass#
You can use values from this NASA fact sheet. You can use whatever units you want, but be consistent.
2) Use your dictionary to look up Earth’s mass#
3) Loop through the data and create a list of planets whose mass is greater than 100 × 10²⁴ kg#
Display your result.
4) Now add pluto to your dictionary#
Part III: Functions#
1) Write a function to convert temperature from Kelvin to Celsius#
…and another to convert from Celsius to Kelvin.
2) Write a function to convert temperature to Fahrenheit#
Include an optional keyword argument to specify whether the input is in Celsius or Kelvin. Call your previously defined functions if necessary.
3) Check that the outputs are sensible#
by trying a few examples
4) Now write a function that converts from Fahrenheit#
Use a keyword argument to specify whether you want the output in Celsius or Kelvin.
5) Write a function that takes two arguments (feet and inches) and returns height in meters#
Verify it gives sensible answers
6) Write a function that takes one argument (height in meters) and returns two values (feet and inches)#
(Consult the tutorial on numbers if you’re stuck on how to implement this.)
7) Verify that the “round trip” conversion from and back to meters is consistent#
Check for 3 different values of height in meters
Submission instructions#
When you’re done, save your completed notebook as assignment3.ipynb inside a new week3/ folder in your private clmt5405-assignments GitHub repo (the one you set up in Assignment 1). Then push the commit:
git add week3/assignment3.ipynb
git commit -m "Submit assignment 3"
git push
Due Sunday night.