Lecture 10.1: Leap Years
Overview
The lesson builds a Python program that validates Gregorian-calendar dates in MM/DD/YYYY form, combining string slicing, integer conversion, conditionals, comparisons, and a Boolean leap-year function. It explains why the Gregorian rule differs from “every four years”, years divisible by 4 are leap years except century years not divisible by 400, and ends by steering students toward checking divisibility by 400 before 100 to simplify the logic.
Key takeaways
- A Gregorian leap year is divisible by 4, except that years divisible by 100 are excluded unless they are also divisible by 400.
- Checking divisibility by 400 before checking 100 simplifies the leap-year decision: multiples of 400 qualify, other multiples of 100 do not, and remaining years qualify when divisible by 4.
- For a fixed MM/DD/YYYY string, Python slices date[0:2], date[3:5], and date[6:] extract month, day, and year; converting each slice with int makes numeric comparisons possible.
- Python chained comparisons such as 1 <= day <= 31 express inclusive numeric ranges directly and more readably than separate greater-than and less-than conditions.
- A date validator can defer leap-year logic until it encounters February 29; ordinary February dates need only the range check 1 through 28.
- Python’s in operator provides a concise way to test month groups, such as membership in the 31-day month set 1, 3, 5, 7, 8, 10, and 12.
Chapters
- The exercise is to determine whether an input string represents a valid Gregorian-calendar date.
- The program practices Python if statements, modulo arithmetic, string indexes, and slices.
- The expected US-style input is month/day/year with two-digit month and day and a four-digit year.
- The Julian calendar uses a leap day every four years, averaging 365.25 days per year.
- The solar year is about 365 days, 5 hours, 48 minutes, and 45 seconds, so the Julian calendar gradually drifted against the seasons.
- The Gregorian rule averages 365.2425 days by skipping most century leap years while retaining years divisible by 400.
- The lecture gives 1600 and 2000 as leap years, but 1700 and 2100 as non-leap years.
- The Gregorian calendar was instituted under Pope Gregory XIII to replace the Julian calendar.
- By the British Empire’s adoption of the Gregorian calendar, Julian dates had drifted by 11 days, explaining historical “old style” and “new style” date labels.
- The calendar correction helps keep seasonal dates, such as harvest periods and winter holidays, aligned with the seasons.
- A slice such as string[start:end] includes the start index but excludes the end index.
- For a date string, indices 0 to 1 hold the month, 3 to 4 the day, and 6 onward the year.
- Omitting a slice endpoint selects through the start or end of the string; an oversized endpoint is safely clipped to the string’s end.
- The proposed parser converts date[0:2], date[3:5], and date[6:] to integers for month, day, and year.
- Converting a string such as "05" to an integer yields 5, removing the leading zero.
- The exercise assumes correctly formatted input, so the initial focus is validating the date values rather than malformed strings.
- The 31-day months are 1, 3, 5, 7, 8, 10, and 12; the 30-day months are 4, 6, 9, and 11.
- February is handled as a separate category because its length depends on leap-year status.
- Python’s in operator makes month checks clearer than repeating month == value clauses joined by or.
- Months outside the range 1 to 12 should produce an invalid-date result.
- For a 31-day month, the day must be between 1 and 31 inclusive; for a 30-day month, it must be between 1 and 30.
- Python supports mathematical chained comparisons such as 1 <= day <= 31.
- The lesson tests boundary cases including day 0, a negative day, November 30, and November 31.
- February 1 to 28 are valid every year, while February 30 and 31 are never valid.
- The program handles day 29 in its own conditional branch rather than burying the leap-year test in a long compound expression.
- That branch calls an is_leap_year(year) function and reports February 29 as valid only when the function returns true.
- The is_leap_year function answers a true-or-false question and takes the year as its input.
- A temporary function that returns false lets the rest of the date validator be tested before leap-year logic is complete.
- The rule requires divisibility by 4, excludes years divisible by 100, and makes an exception for years divisible by 400.
- Checking divisibility by 4 first and then nesting the century exceptions can create tangled conditional logic.
- The suggested alternative is to check divisibility by 400 first: those years are leap years.
- If a year is not divisible by 400 but is divisible by 100, it is not a leap year; otherwise, divisibility by 4 determines the result.
- The lesson ends with this reordering as a student task rather than showing a completed final implementation.
Transcript excerpt
0:00 All right, start recording. And we've got signal from me talking. Great. Okay, so today we are going to continue working on uh another in-class exercise. This one is a another form of homework. We've also done this as a experiment sometimes that we've run in class where we use this as part of an experiment. It's a lot of fun. As I was saying, uh the name is inest. So, if you're um so you're going to learn a bit of Catholic history today um just because we're talking about papal stuff, but we're also going to learn a bit of astronomy and stuff. Again, like the previous one, this one is uh check for participation credit. So, you turn in what you've done. Um this one's also one that I grabbed from Savage's book. And I like this one
0:47 because it's non-trivial for every level of programmer. Okay, it is gonna primarily cover our if statements, um our module arithmetic arithmetic. We're going to do our string indexes um and it's adapted from another problem I used to do in Java from Savage's Java textbook. So what we're going to do today is we are going to take a we're going to write a program that given a string as input tests if the string is valid in the a valid date in the Gregorian calendar. Okay. Now that's very technical terms to say is it a valid date but like also at the same time it's not. We're checking if it's valid in the Cregorian calendar
1:35 because there's other calendar systems right? If you're Muslim, you have your, you know, you have the Islamic calendar that you follow. If you're Jewish like me, there's a Hebrew calendar as well, right? If you're from uh if you happen
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Professor Rosen.