Day -10 Determining Days in a Month and Handling Leap Years with Python

Day -10 Determining Days in a Month and Handling Leap Years with Python

100 Days Of Code - Python

Introduction

In this blog post, we will explore a Python code snippet that calculates the number of days in a given month and handles leap years. The code utilizes two functions, is_leap() and days_in_month(), to provide the desired output. Let's dive into the code and understand how it works.

The code begins with the definition of the is_leap() function. This function takes a parameter year, which represents a year. It determines whether the given year is a leap year or not by following these steps:

  1. It first checks if the year is divisible evenly by 4 using the modulo operator %.

  2. If the year is divisible by 4, it proceeds to the next step and checks if it is also divisible by 100.

  3. If the year is divisible by 100, it further checks if it is divisible by 400.

  4. If the year passes all three conditions, i.e., it is divisible by 4, not divisible by 100, or divisible by 400, then it is considered a leap year, and the function returns True. Otherwise, it returns False.

def is_leap(year):
    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                return True
            else:
                return False
        else:
            return True
    else:
        return False

Moving on, the code defines the days_in_month() function, which takes two parameters: years and months. Inside this function, a list of month_days is created, representing the number of days in each month of a non-leap year.

The function then checks if the given year is a leap year by invoking the is_leap() function with the years parameter and verifying if the months parameter is equal to 2 (representing February). If both conditions are true, it returns 29, indicating that February has 29 days in a leap year.

def days_in_month(years, months):
    month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    if is_leap(years) and months == 2:
        return 29
    return month_days[month - 1]

Finally, if the given year is not a leap year or the input month is not February, the function returns the number of days from the month_days list corresponding to the input month.

In the remaining part of the code, the user is prompted to enter a year and a month. The days_in_month() function is called with the input values, and the resulting number of days is stored in the days variable. Finally, the value of days is printed to the console.

year: int = int(input("Enter year: "))
month = int(input("Enter month: "))
days = days_in_month(years=year, months=month)
print(days)

Expected Output:

When executing the code and providing valid input values, the output will be the number of days in the specified month of the given year. For example, if the user enters a year of 2022 and a month of 3, the output will be 31, representing that March has 31 days.

Conclusion:

In this blog post, we have explored a Python code snippet that calculates the number of days in a given month while considering leap years. By utilizing the is_leap() and days_in_month() functions, we can accurately determine the number of days for any month and year combination. This code can be useful in various scenarios where handling dates and calculating time-related information is required.

#python #100daysofcode