Assignment: Basic Python#

Learning Goals

This assignment will verify that you have the following skills:

  • Create lists and dictionaries

  • Iterate through lists, tuples, and dictionaries

  • Index sequences (e.g. lists and tuples)

  • Define functions

  • Use optional keyword arguments in functions

Part I: Lists and Loops#

In this problem, we will explore the basic data structures and flow controls of Python by manually creating data structures.

Warning

Pluto is not a planet any more.

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 mast is greater than 100 x 10^24 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 celsius to kelvin

2. Write a function to convert temperature to fahrenheit#

Include an optional keyword argument to specify whether the input is in celcius 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 farenheit#

and uses a keyword argument to specify whether you want the output in celcius 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 takes one argument (height in meters) and returns two arguments (feet and inches)#

(Consult the tutorial on numbers if you are 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