Python Decision Making

Python program to check even or odd number

Description: Write a Python program to check whether a number is even or odd.

Rules and hints:

  • A number is even if it is divisible by 2.
  • Use the modulus operator %, which gives the remainder.

Python program to check even or odd

for number in (4, 7):
    if number % 2 == 0:
        print(number, "is even")
    else:
        print(number, "is odd")

Output:

Tutorials Class - Output Window

4 is even
7 is odd

How it works

  • 4 % 2 is 0, so 4 is even.
  • 7 % 2 is 1, so the else part runs and 7 is odd.
  • Negative numbers also work, because -3 % 2 is 1 in Python.

Learn the basics in Python Conditional Statements.

Python program to find the largest of three numbers

Description: Write a Python program to find the largest of three numbers.

Rules and hints:

  • First use if, elif and else.
  • Then try the built-in max() function.

Python program to find the largest of three numbers

a = 25
b = 78
c = 42

if a >= b and a >= c:
    largest = a
elif b >= a and b >= c:
    largest = b
else:
    largest = c

print("Largest number is", largest)
print("Using max():", max(a, b, c))

Output:

Tutorials Class - Output Window

Largest number is 78
Using max(): 78

How it works

  • The first condition checks if a is bigger than or equal to both others.
  • If not, the elif checks b. If both fail, c must be the largest.
  • max() does all this work in one call, so use it in real programs.

Python program to check leap year

Description: Write a Python program to check whether a year is a leap year.

Rules and hints:

  • A year is a leap year if it is divisible by 4 but not by 100.
  • A year divisible by 400 is also a leap year.

Python program to check a leap year

def is_leap(year):
    return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0

for year in (2024, 1900, 2000, 2023):
    if is_leap(year):
        print(year, "is a leap year")
    else:
        print(year, "is not a leap year")

Output:

Tutorials Class - Output Window

2024 is a leap year
1900 is not a leap year
2000 is a leap year
2023 is not a leap year

How it works

  • 2024 is divisible by 4 and not by 100, so it is a leap year.
  • 1900 is divisible by 100 but not by 400, so it is not a leap year.
  • 2000 is divisible by 400, so it is a leap year.
  • 2023 is not divisible by 4.

Python calculator program using functions

Description: Write a simple calculator in Python using a function. It should support +, -, * and /.

Rules and hints:

  • Write one function that takes two numbers and an operator.
  • Check for division by zero.
  • Return a message for a wrong operator.

Python calculator program

def calculate(a, b, op):
    if op == "+":
        return a + b
    elif op == "-":
        return a - b
    elif op == "*":
        return a * b
    elif op == "/":
        if b == 0:
            return "Cannot divide by zero"
        return a / b
    else:
        return "Invalid operator"

print(calculate(10, 5, "+"))
print(calculate(10, 5, "-"))
print(calculate(10, 5, "*"))
print(calculate(10, 5, "/"))
print(calculate(10, 0, "/"))

Output:

Tutorials Class - Output Window

15
5
50
2.0
Cannot divide by zero

How it works

  • The function checks the operator with if and elif and returns the answer.
  • A division by zero would crash the program, so we check b == 0 first.
  • You can read the numbers and the operator with input() to make it interactive.

Related: Python Functions.