Exercises

Here are some exercises for you to try on your own. Solutions are available at http://pragprog.com/titles/gwpy3/practical-programming.

  1. Import module math, and use its functions to complete the following exercises. (You can call dir(math) to get a listing of the items in math.)

    1. Write an expression that produces the floor of -2.8.
    2. Write an expression that rounds the value of -4.3 and then produces the absolute value of that result.
    3. Write an expression that produces the ceiling of the sine of 34.5.
  2. In the following exercises, you will work with Python’s calendar module:

    1. Visit the Python documentation website at http://docs.python.org/release/3.6.0/py-modindex.html, and look at the documentation on module calendar.
    2. Import module calendar.
    3. Using function help, read the description of function isleap.
    4. Use isleap to determine the next leap year.
    5. Use dir to get a list of what calendar contains.
    6. Find and use a function in module calendar to determine how many leap years there will be between the years 2000 and 2050, inclusive.
    7. Find and use a function in module calendar to determine which day of the week July 29, 2016, will be.
  3. Create a file named exercise.py with this code inside it:

     def​ average(num1: float, num2: float) -> float:
     """Return the average of num1 and num2.
     
      >>> average(10,20)
      15.0
      >>> average(2.5, 3.0)
      2.75
      """
     
     return​ num1 + num2 / 2
    1. Run exercise.py. Import doctest and run doctest.testmod().

    2. Both of the tests in function average’s docstring fail. Fix the code and rerun the tests. Repeat this procedure until the tests pass.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.184.200