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.8 LAB: Day of the year: writing and using your own functions

 Day of the year: writing and using your own functions

4.3.1.8 LAB
Day of the year: writing and using your own functions

Prerequisites

LAB 4.3.1.6

LAB 4.3.1.7

Objectives

Familiarize the student with:

  • projecting and writing parameterized functions;
  • utilizing the return statement;
  • building a set of utility functions;
  • utilizing the student's own functions.

Scenario

Your task is to write and test a function which takes three arguments (a year, a month, and a day of the month) and returns the corresponding day of the year, or returns None if any of the arguments is invalid.

Use the previously written and tested functions. Add some test cases to the code. This test is only a beginning.


Stoooooooop .. we are here to real the REAL PYTHON, not this SILLY COURSE

Dear learner  .. if you reached here, that means you'd have had enough of this boring course already😏. Let's learn python the right way, not the old school way.

Solution Code:

def is_leap_year(year): # LAB 4.3.1.6 A leap year
    if year % 4 != 0:
        return False
    elif year % 100 != 0:
        return True
    elif year % 400 != 0:
        return False
    else:
        return True


def days_in_month(year, month): #LAB 4.3.1.7 How many days
    if year < 1 or month < 1 or month > 12:
        return None
    days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    res  = days[month - 1]
    if month == 2 and is_leap_year(year):
        res = 29
    return res


def day_of_year(year, month, day): #LAB 4.3.1.8 Day of the year
    days = 0
    for m in range(1, month):
        md = days_in_month(year, m)
        if md == None:
            return None
        days += md
    md = days_in_month(year, month)
    if day >= 1 and day <= md:
        return days + day
    else:
        return None

print(is_leap_year(1900))
print(days_in_month(2021, 11))
print(day_of_year(2000,2,29))

Output:

  • False
  • 30
  • 60

=================================================================================

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






Building Utility Functions with the return Statement

This lab focuses on projecting and writing parameterized functions and combining them into a small set of utilities that work together. It builds directly on two earlier labs: is_leap_year(year) from LAB 4.3.1.6 and days_in_month(year, month) from LAB 4.3.1.7. Each function takes arguments through its parameters, performs a focused job, and hands a result back to the caller through the return statement.

The heart of the exercise is day_of_year(year, month, day), which returns the corresponding day of the year, or None if any argument is invalid. The function works by accumulating the total number of days from every complete month before the requested one. It loops over range(1, month), and for each month m it calls days_in_month(year, m) to discover how many days that month has, adding them to a running total.

Handling Invalid Input Gracefully

The correctness of this code depends on careful validation at every level. First, days_in_month returns None if the year is below one or the month falls outside 1..12. Because February can have either 28 or 29 days, it consults the leap-year function through the test month == 2 and is_leap_year(year) before returning a value from the days list. Second, day_of_year checks that the requested day is between 1 and the number of days in that actual month; if the day is out of range, both the month total and the day are rejected and None is returned. In the verification output, 2021-11-30 maps to day 30 of November, and 2000-02-29 produces day 60 because February 2000 is a leap year.

Key Takeaways

  • Write small, single-purpose functions and reuse them, so each one is easy to test in isolation.
  • Use the return statement to send computed values back to the calling code.
  • Return None to signal invalid input such as a bad year, month, or day.
  • days_in_month(year, month) adjusts February to 29 only when is_leap_year returns True.
  • Accumulate days by summing the lengths of all months before the target month.
  • Add your own test cases, as the lab reminds you that testing is only a beginning.

Frequently Asked Questions

Why does the order of the days in the list matter?

The list stores days per month by index, so days_in_month uses month - 1 to convert a 1-based month number into the correct 0-based position in the list.

What does returning None accomplish?

It signals that the inputs are invalid, allowing the caller to distinguish a real day-of-year value from a failed lookup.

Why must February be handled specially?

Because February has 29 days in leap years and 28 otherwise, so its result depends on the outcome of is_leap_year(year).

How is the final day added to the total?

After summing all previous months, the function validates the requested day and, if valid, returns the accumulated total plus that day.

Comments

Popular Posts

PROJECT MAVEN | The Architecture of Algorithmic Warfare

Open Source: The Invisible Engine of Your Daily Life

How to Deactivate Screen Reader in Kali Linux

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

Convert a Currency Number to Words in Excel