📖
Introduction to programming with Python
  • Introduction to programming with Python 3
  • Preface
    • Common misconceptions about learning to program
    • The Hard truth about learning to program
    • Personal qualities for success
  • 1 - The way of the program
    • Python Programming Language
    • The first program
    • What is debugging?
    • Glossary
    • Exercises
  • 2 - Variables, expressions and statements
    • Values, types and variables
    • Common Built-in data types
    • Expressions
    • Code Format
    • Debugging
    • Glossary
    • Exercises
  • 3 - Functions
    • Python "built-in" Functions
    • Math Functions
    • Composition
    • User Defined Functions
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 4 - Conditionals
    • Boolean expressions
    • Conditional statements
    • PEP 8 Recommendations
  • 5 - Iteration
    • The while statement
    • The for statement
    • break and continue statements
    • Nested Loops
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 6 - A short introduction to testing: Building Reliable Software
  • 7 - A deeper dive into Strings, Lists and Tuples
    • More on Strings
    • More on Lists
    • More on Tuples
    • Debugging
    • Glossary
    • Exercises
  • 8 - A deeper look at Functions in Python
    • Function Preconditions and Postconditions
    • Positional and Keywords arguments
    • Nested Functions
    • Scope of a variable
    • Recursion
    • Functions' side effects
    • Glossary
    • Exercises
  • 9 - Code Documentation
    • Basics of Commenting Code
    • Documenting Code via Python Docstring
  • 10 - Sets and dictionaries
    • Sets
    • Dictionaries
    • Which data structure should I use?
    • Debugging
    • Glossary
    • Exercises
  • 11 - File I/O
    • Read/Write to a file
    • File management
    • Debugging
    • Glossary
    • Exercises
  • 12 - Handling Errors and Exceptions
  • 13 - Writing modules
  • 14 - Classes
    • Classes and Objects
    • Classes and Functions
    • Classes and Methods
    • Pythonic Magic: Understanding and Implementing Dunder Methods
    • Glossary
    • Exercises
  • 15 - Python's Type Hinting
  • Acknowledgements
Powered by GitBook
On this page
  • Exercise 1:
  • Exercise 2:
  • Exercise 3:
  • Exercise 4:
Edit on GitHub
  1. 14 - Classes

Exercises

PreviousGlossaryNext15 - Python's Type Hinting

Last updated 1 year ago

Exercise 1:

Write a function called mul_time that takes a Time object and a number and returns a new Time object that contains the product of the original Time and the number.

Then use mul_time to write a function that takes a Time object that represents the finishing time in a race, and a number that represents the distance, and returns a Time object that represents the average pace (time per mile).

Exercise 2:

Write a class definition for a Date object that has attributes day, month and year. Write a function called increment_date that takes a Date object, date and an integer, n, and returns a new Date object that represents the day n days after date.

"Thirty days hath September..." Challenge: does your function deal with correctly?

Exercise 3:

The datetime module provides date and time objects that are similar to the Date and Time objects in this chapter, but they provide a rich set of methods and operators. Read the .

  • Use the datetime module to write a program that gets the current date and prints the day of the week.

  • Write a program that takes a birthday as input and prints the user's age and the number of days, hours, minutes and seconds until their next birthday.

Exercise 4:

This exercise is a cautionary tale about one of the most common, and difficult to find, errors in Python.

  • Write a definition for a class named Kangaroo with the following methods:

    • An __init__ method that initialises an attribute named pouch_contents to an empty list.

    • A method named put_in_pouch that takes an object of any type and adds it to pouch_contents.

    • A __str__ method that returns a string representation of the Kangaroo object and the contents of the pouch.

    Test your code by creating two Kangaroo objects, assigning them to variables named kanga} and roo, and then adding roo to the contents of kanga's pouch.

  • Download . It contains a solution to the previous problem with one big, nasty bug. Find and fix the bug.

If you get stuck, you can download , which explains the problem and demonstrates a solution.

leap years
documentation of both objects
BadKangaroo.py
GoodKangaroo.py