PyForge ยท Python practice library
All topics

Topic 03 of 17

Control Flow

Make decisions with if / elif / else and master conditional logic.

if / elif / else
Ternary expressions
match / case
Truthy & falsy

Basic

2 programs

Program 1

Even or odd

Basic

Check if a number is even or odd.

python
n = int(input("Enter a number: "))
if n % 2 == 0:
    print(n, "is even")
else:
    print(n, "is odd")

Program 2

Largest of three

Basic

Find the largest using if/elif.

python
a, b, c = 10, 25, 17
if a >= b and a >= c:
    print(a)
elif b >= c:
    print(b)
else:
    print(c)

Intermediate

1 program

Program 1

Grade calculator

Intermediate

Map scores to letter grades.

python
score = 82
grade = (
    "A" if score >= 90 else
    "B" if score >= 80 else
    "C" if score >= 70 else
    "D" if score >= 60 else "F"
)
print("Grade:", grade)

Advanced

1 program

Program 1

Pattern matching (match/case)

Advanced

Python 3.10+ structural pattern matching.

python
def describe(point):
    match point:
        case (0, 0):
            return "Origin"
        case (x, 0):
            return f"On x-axis at {x}"
        case (0, y):
            return f"On y-axis at {y}"
        case (x, y):
            return f"Point ({x}, {y})"

print(describe((0, 0)))
print(describe((3, 0)))
print(describe((2, 5)))