Simple Python Programs for Beginners

Here are two simple Python programs that cover basic programming concepts. The first program determines whether a given year is a leap year or not, and the second program finds out the greater of two numbers input by the user.


1. Program to Find Out if a Year is a Leap Year

This program prompts the user to input a year and then checks if it is a leap year using basic conditional statements.

Code:

# Leap Year Checker

# Function to check if the year is a leap year
def is_leap_year(year):
    if (year % 4 == 0):
        if (year % 100 == 0):
            if (year % 400 == 0):
                return True  # Divisible by 400, hence leap year
            else:
                return False  # Divisible by 100 but not by 400, not a leap year
        else:
            return True  # Divisible by 4 but not by 100, hence leap year
    else:
        return False  # Not divisible by 4, hence not a leap year

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

# Check and print whether it is a leap year or not
if is_leap_year(year):
    print(f"{year} is a leap year.")
else:
    print(f"{year} is not a leap year.")

Simplified Program to Find Out if a Year is a Leap Year

Certainly! Here are simplified versions of the two programs without using functions:


1. Simplified Program to Find Out if a Year is a Leap Year

Code:

# Leap Year Checker

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

# Check if the year is a leap year
if (year % 4 == 0):
    if (year % 100 == 0):
        if (year % 400 == 0):
            print(f"{year} is a leap year.")
        else:
            print(f"{year} is not a leap year.")
    else:
        print(f"{year} is a leap year.")
else:
    print(f"{year} is not a leap year.")

Explanation:

  • This program directly checks the conditions for leap year determination using nested if-else statements.

  • It first checks if the year is divisible by 4. If true, it then checks if it is also divisible by 100. If true, it further checks if it is divisible by 400 to confirm it is a leap year.


2. Program to Find the Greater of Two Numbers

This program prompts the user to input two numbers and determines which number is greater.

Code:

# Greater Number Finder

# Function to find the greater of two numbers
def find_greater_number(num1, num2):
    if num1 > num2:
        return num1
    else:
        return num2

# User inputs
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Find and print the greater number
greater_number = find_greater_number(num1, num2)
print(f"The greater number between {num1} and {num2} is {greater_number}.")

Certainly! Here are simplified versions of the two programs without using functions:


2. Simplified Program to Find Out if a Year is a Leap Year

Code:

# Leap Year Checker

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

# Check if the year is a leap year
if (year % 4 == 0):
    if (year % 100 == 0):
        if (year % 400 == 0):
            print(f"{year} is a leap year.")
        else:
            print(f"{year} is not a leap year.")
    else:
        print(f"{year} is a leap year.")
else:
    print(f"{year} is not a leap year.")

Multiple Interpretations:

  • Basic Interpretation: The program uses a simple if-else statement to compare the two numbers. If the first number is greater, it is returned; otherwise, the second number is returned.

  • Detailed Explanation: The comparison operator > is used to check which number is greater. The float() function is used to handle both integer and decimal inputs, ensuring flexibility in the comparison.


These programs introduce fundamental programming concepts such as user input, conditional statements, and basic functions. Feel free to modify and experiment with these programs to enhance your understanding of Python programming!