Assignment 2: Python Functions and Classes

Part I: Exploring the Python Standard Library

Skim the documentation for the datetime module

1. Import the datetime module

In [ ]:
 

2. Create a datetime object for the day you were born

and print its representation in your notebook

In [ ]:
 

3. Create a timedelta object representing 100 days, 10 hours, and 13 minutes

In [ ]:
 

4. Verify that these two objects do not have the same type

Use an assert statement

In [ ]:
 

5. Add the timedelta to the datetime

and assign it to a new variable

In [ ]:
 

6. Display the month of the new date

In [ ]:
 

7. Create a list of datetimes for all of your birthdays

beginning from your 1st birthday and ending on your most recent birthday. (Don't do this manually; use a loop or a list comprehension!)

In [ ]:
 

8. Count how many of your birthdays occured on a Tuesday

Don't forget about the docs!

In [ ]:
 

Part II: Writing Functions

1. Write a function to convert temperature from kelvin to celsius

and celsius to kelvin

In [ ]:
 

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.

In [ ]:
 

3. Check that the outputs are sensible

by trying a few examples

In [ ]:
 
In [ ]:
 

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

In [ ]:
 

5. Write a function that takes two arguments (feet and inches) and returns height in meters

Verify it gives sensible answers

In [ ]:
 
In [ ]:
 

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.)

In [ ]:
 
In [ ]:
 

7. Verify that the "round trip" conversion from and back to meters is consistent

Check for 3 different values of height in meters

In [ ]:
 

Part III: Modules

[This section is worth 6 points]

Put all of your above functions into a module called units.py. Create valid docstrings for each function.

Import the module. Verify that you can call the functions and view their docstrings.

In [ ]:
 

Part IV: Classes

[This section is worth 6 points]

Write a class that represents a Planet. The constructor class should accept the arguments radius (in meters) and rotation_period (in seconds).

You should implement three methods:

  • surface_area
  • rotation_frequency
  • __eq__ "magic/dunder method" used to test for equality between two planets (i.e. planet1 == planet2)
In [ ]:
 

If you class works properly, it should pass all of the tests below:

In [ ]:
earth = Planet(6.3781e6, 86400)

import pytest
assert earth.surface_area() == pytest.approx(5.112015e14)
assert earth.rotation_frequency() == pytest.approx(7.2722e-5)

earth2 = Planet(6.3781e6, 86400)
assert earth == earth2

jupyter = Planet(69.911e6, 9.925*60*60)
assert jupyter.surface_area() == pytest.approx(6.14187e16)
assert jupyter.rotation_frequency() == pytest.approx(1.758518e-4)
In [ ]: