datetime addition

Δίσεκτο έτος
Δίσεκτο έτος στη διαδρομή Python
from datetime import datetime, timedelta


def leap_year(year):
    return (datetime(year, 2, 28)
            + timedelta(days=1)).day == 29
Note

This approach may be considered a "cheat" for this exercise, which is intended to practice Boolean operators and boolean logic. It also adds overhead in both performance and memory, as it imports methods from the datetime module and requires the instantiation of both a datetime object and a timedelta object.

For more information, see the performance article for this exercise.

By adding a day to February 28th for a given year, you can see if the new day falls on the 29th of February or the 1st of March. If it is February 29th, then the function returns True for the year being a leap year. The exact steps are as follows:

  • A new datetime object is created for February 28th of the year.
  • A timedelta of one day is added to that datetime.
  • The function returns if the day property of the resulting datetime object is the 29th.
Translation missing: el.number.nth.ordinalized Sep 2026 · Σου φάνηκε χρήσιμη;