Review Exercise
Last updated on 2025-02-10 | Edit this page
Overview
Questions
- How can we put together all of yesterday’s material?
Objectives
- Apply use of functions, conditionals and loops to solve a problem.
Review From Yesterday - All in One Exercise
In your notebook, write a function that determines whether a year between 1901 and 2000 is a leap year, where it prints a message like “1904 is a leap year” or “1905 is not a leap year” as output. Use this function to evaluate the years 1928, 1950, 1959, 1972 and 1990. Essentially, given this list of years:
Produce something like:
OUTPUT
1928 is a leap year
1950 is not a leap year.
1959 is not a leap year.
1972 is a leap year
1990 is not a leap year.
Hint: the percent symbol ‘%’ is the modular operator in Python. So:
OUTPUT
8 mod 4 equals 0
10 mod 4 equals 2
If you’re not sure where to start, see the fill-in-the-blank version of this exercise below.
Review From Yesterday - Step by Step Breakdown
First, try to determine how to use the mod operator %
to
determine if a year is divisible by 4 (and thus a leap year or not).
If a year in the range specified is divisible by four, it is a leap
year. If a number is divisible by 4, then the arithmetic expression
“number mod four” (or num % 4
in Python) will equal
zero.
Review From Yesterday - Step by Step Breakdown (continued)
Then, create a conditional statement to use this information, and put it into a function.
Review From Yesterday - Step by Step Breakdown (continued)
Then, create a list of the years given in the exercise. Use a for loop and your function to evaluate these years.
Review From Yesterday - Step by Step Breakdown (continued)
Finally, use a for loop and your function to evaluate these years.
Additonal Challenge
If you have time:
Expand your function so that it correctly categorizes any year from 0 onwards
Instead of printing whether a year is a leap year or not, save the results to a python dictionary, where there are two keys (“leap” and “not-leap”) and the values are a list of years.
Key Points
- Use skills together.