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...

Python3.1.1.12 LAB: Essentials of the if-elif-else statement

Python 3.1.1.12 LAB: Essentials of the if-elif-else statement

Python 3.1.1.12 LAB flow chart and VS Code solution for if-elif-else leap year program
if-elif-else

If you're taking PCAP - Programming Essentials In Python , you may have encountered this question in 3.1.1.12 LAB: Essentials of the if-elif-else statement:

Scenario

As you surely know, due to some astronomical reasons, years may be leap or common. The former are 366 days long, while the latter are 365 days long.

Since the introduction of the Gregorian calendar (in 1582), the following rule is used to determine the kind of year:
  • if the year number isn't divisible by four, it's a common year;
  • otherwise, if the year number isn't divisible by 100, it's a leap year;
  • otherwise, if the year number isn't divisible by 400, it's a common year;
  • otherwise, it's a leap year.
Look at the code in the editor - it only reads a year number, and needs to be completed with the instructions implementing the test we've just described.

The code should output one of two possible messages, which are Leap year or Common year, depending on the value entered.

It would be good to verify if the entered year falls into the Gregorian era, and output a warning otherwise: Not within the Gregorian calendar period. Tip: use the != and % operators.

Test your code using the data we've provided.
Test Data

Sample input: 2000

Expected output: Leap year

Sample input: 2015

Expected output: Common year

Sample input: 1999

Expected output: Common year

Sample input: 1996

Expected output: Leap year

Sample input: 1580

Expected output: Not within the Gregorian calendar period

Solution Code:

year = int(input("Enter a year: "))

if year < 1852: #year does NOT fall into Gregorian era
    print("Not within the Gregorian calendar period")
elif int(year % 4) != 0: #year number isn't divisible by 4
    print("Common year")
elif int(year % 100) != 0: #year number isn't divisible by 100
    print("Leap year")
elif int(year % 400) != 0: #year number isn't divisible by 400
    print("Common year")
else:
    print("Leap year")
------------------------------------------------------------------------------

Deciding Leap Years with Decision Statements

This lab practices the essentials of the if-elif-else statement by implementing the Gregorian calendar rule for deciding whether a year is a leap year or a common year. A leap year has 366 days while a common year has 365. Since 1582 the rule works through a strict sequence: if the year is not divisible by four it is common; otherwise, if it is not divisible by one hundred it is leap; otherwise, if it is not divisible by four hundred it is common; and otherwise it is leap.

The solution relies on the modulo operator to test divisibility. The expression year % 4 produces the remainder of dividing by four, so a remainder of zero means the year is divisible by four. By comparing that remainder with != 0, each branch of the if-elif-else chain decides which message to print. The first check is separate: if the year falls before the start of the Gregorian era, the program warns with "Not within the Gregorian calendar period" instead of classifying it at all.

Ordering the Checks Correctly

The order of the branches is essential. Checking the century and four-hundred rules first would break the logic, because every multiple of four hundred is also a multiple of one hundred and four. The solver therefore checks each condition in increasing strictness, allowing a value such as 2000 to pass through as a leap year while 1900, divisible by 100 but not 400, is correctly reported as a common year. The sample tests confirm the behavior: 2000 and 1996 are leap years, 2015 and 1999 are common years, and 1580 falls outside the Gregorian period.

Key Takeaways

  • Use year % N != 0 to test whether a year is not divisible by N.
  • The rule follows a fixed order: divisible by 4, then 100, then 400.
  • A year divisible by 4 but not 100 is a leap year.
  • A year divisible by 100 but not 400 is a common year (for example, 1900).
  • A year divisible by 400 is always a leap year.
  • Verify the entered year is inside the Gregorian calendar period and warn otherwise.

Frequently Asked Questions

Why must the checks run in exactly this order?

Because the tests are nested in difficulty; a century year is also divisible by four, so the four-hundred test must come after the other divisibility checks to avoid misclassifying it.

What does year % 400 != 0 actually mean?

It means the remainder when dividing by 400 is not zero, so the year is not divisible by 400 and therefore cannot be a leap year.

Why is 1900 a common year but 2000 a leap year?

Both are divisible by 100, but 2000 is also divisible by 400, while 1900 is not, so 2000 satisfies the strictest test and becomes a leap year.

What happens for a year before 1582?

The program detects the value with an early check and prints "Not within the Gregorian calendar period" rather than trying to classify it.

Comments

Popular Posts

PROJECT MAVEN | The Architecture of Algorithmic Warfare

Python 3.2.1.14 LAB: Essentials of the while loop

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

Open Source: The Invisible Engine of Your Daily Life

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