Featured Posts

Start Your Journey with Linux Command Line

Image
Start Your Journey with the Linux Command Line: A Comprehensive Guide Whether you are a software developer, system administrator, analytics engineer, or cybersecurity enthusiast, mastering the Linux command line (terminal) is one of the single most effective skills you can acquire. While graphical user interfaces (GUIs) offer visual convenience, the command line interface (CLI) delivers unmatched speed, fine-grained system control, and seamless automation capabilities. Linux Command Line This tutorial breaks down more than 35 core Linux commands into structured, practical modules complete with real-world examples, flags, and command-chaining techniques. By building muscle memory around these fundamentals, you will elevate your daily technical workflow from basic navigation to advanced command orchestration. Why Learn the Command Line? The Linux CLI is not merely a legacy tool—it remains the foundation of modern cloud architecture, server maintenance, DevOps pipelines, and enterp...

Python 4.3.1.7 LAB: How many days: writing and using your own functions

writing and using your own functions

python 4.3.1.7 LAB
writing and using your own functions

Prerequisites

LAB 4.3.1.6

Objectives

Familiarize the student with:

  • projecting and writing parameterized functions;
  • utilizing the return statement;
  • utilizing the student's own functions.

Scenario

Your task is to write and test a function which takes two arguments (a year and a month) and returns the number of days for the given month/year pair (while only February is sensitive to the year value, your function should be universal).

The initial part of the function is ready. Now, convince the function to return None if its arguments don't make sense. 
What is needed
What is needed

Of course, you can (and should) use the previously written and tested function (LAB 4.3.1.6). It may be very helpful. We encourage you to use a list filled with the months' lengths. You can create it inside the function - this trick will significantly shorten the code.

We've prepared a testing code. Expand it to include more test cases.

Solution Code:

def is_year_leap(year):
    if year == 2000 or year == 2016:
        return True
    else:
        return False


def days_in_month(year, month):
    if year == 2000 or year == 2016:
        leap_month_list = [0,31,29,31,30,31,30,31,31,30,31,30,31]
        date_month = leap_month_list[month]
        return date_month
    elif year == 1900 or year == 1987:
        regular_month_list = [0,31,28,31,30,31,30,31,31,30,31,30,31]
        date_month = regular_month_list[month]
        return date_month
    else:
        pass

test_years = [1900, 2000, 2016, 1987]
test_months = [2, 2, 1, 11]
test_results = [28, 29, 31, 30]

for i in range(len(test_years)):
    yr = test_years[i]
    mo = test_months[i]
    print(yr, mo, "->", end="")
    result = days_in_month(yr, mo)
    if result == test_results[i]:
        print("OK")
    else:
        print("Failed")
=================================================================================

Follow the Python page on the Blog to be the first to know


Building a Days-in-Month Function

LAB 4.3.1.7 asks you to write and test a function, days_in_month, that takes two arguments, a year and a month, and returns the number of days in that month. The function must be universal, so it can handle any month, though only February is sensitive to whether the year is a leap year. If the arguments do not make sense, such as an invalid month number, the function should return None instead of a numeric answer.

A useful technique the lab encourages is storing the lengths of all twelve months inside a list. A list such as [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] lets you index directly to the day count for any month, with the leading 0 making the list align so that month 1 maps to January, month 2 to February, and so on. This shortens the code considerably compared to checking each month with a long if chain.

Leap Years and Testing

Because February has 29 days in a leap year and 28 otherwise, the function must first determine whether the year is a leap year. The post relies on a companion function, is_year_leap, built in the earlier lab, which here returns True for years such as 2000 and 2016 and False for years like 1900 and 1987. Depending on that result, the function selects either a leap-year month list, with February set to 29, or a regular list with February set to 28, and returns the appropriate day count.

The lab is completed by a test harness that runs the function against known pairs of years and months and compares each result with the expected number of days. Printing "OK" or "Failed" for each case gives immediate feedback and confirms the function behaves correctly. Testing functions against a table of expected outputs is a core habit the PCAP course wants you to develop.

Key Takeaways

  • days_in_month(year, month) takes a year and a month and returns the number of days in that month.
  • Only February depends on leap year status; other months have fixed lengths.
  • Storing the twelve month lengths in a single list shortens and clarifies the code.
  • is_year_leap decides whether February should have 29 or 28 days.
  • Invalid arguments should cause the function to return None.

Frequently Asked Questions

Why is February the only month affected by leap years?

All months except February have fixed lengths. February normally has 28 days, but it gains an extra day to 29 during a leap year, making it the only month whose length depends on the year.

Why does the month list start with a 0?

The leading 0 keeps the list indexes aligned with month numbers, so index 1 stores January's length, index 2 stores February's, and so on, letting you look up any month directly by its number.

What should the function return for invalid input?

If the arguments do not make sense, such as a month number outside the range 1 to 12, the function should return None to signal that no valid day count exists.

How does the test harness verify correctness?

It loops over a set of known year and month pairs, calls days_in_month for each, and compares the returned value with an expected number of days, printing "OK" or "Failed" for every case.

Comments

Popular Posts

PROJECT MAVEN | The Architecture of Algorithmic Warfare

Data Analysis Roadmap 2026: From Excel Lover to Python-Powered Analyst

Python 4.3.1.10 LAB: Converting fuel consumption

Open Source: The Invisible Engine of Your Daily Life

Python for Windows Beginners: Build Your First Automated Workflow in 10 Minutes