Preplp
Daily Python
Day 4Python~30 min

Decisions with if/else

Compare values and branch your code

Track progress0/15 days
x

What you'll learn

ifelifelseandorcomparison operators
  • Write if / elif / else blocks
  • Combine conditions with and and or
  • Compare two numbers and explain the result
  • Detect when a number is zero

Lesson & examples

Read the code, then the explanation — try changing values before the project.

if / elif / else
number = 4

if number > 5:
    print("Greater than 5.")
elif number == 5:
    print("Equal to 5.")
else:
    print("Less than 5.")
Logical operators
a, b = 2, 20
if a > 5 or b < 15:
    print("At least one condition is true")

Today's exercise: Number Comparison Tool

Ask for two numbers, compare them, and report whether either value is zero.

Steps

  1. 1

    Read two numbers

    Use float(input()) for num1 and num2.

  2. 2

    Compare them

    Use if / elif / else to print which is greater, or if they're equal.

  3. 3

    Check for zero

    Use or to test if num1 == 0 or num2 == 0 and print an extra message.

Full project source — copy into a folder or use the zip above

starter.py
# Number Comparison Tool

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

print("\n--- Comparison Results ---")

if num1 == num2:
    print(f"Both numbers are equal: {num1}")
elif num1 > num2:
    print(f"{num1} is greater than {num2}")
else:
    print(f"{num2} is greater than {num1}")

if num1 == 0 or num2 == 0:
    print("\nAt least one number is zero.")
else:
    print("\nBoth numbers are non-zero.")

Code explained

  1. 1.if / elif / else chain

    Only one branch runs. First equality is checked, then greater-than; else must be the remaining case.

  2. 2.or for zero check

    num1 == 0 or num2 == 0 is True if either value is zero — a separate block from the comparison above.

Sample output

Enter the first number: 8
Enter the second number: 3

--- Comparison Results ---
8.0 is greater than 3.0

Both numbers are non-zero.

One rehearsal platform

Certification mocks, daily lessons, project labs, and in-browser drills

Structured for exam day and portfolio proof — timed tests, guided builds, and quick reps on one platform.