Lab · School of Computer Science & Technology

Lab 3 — Functions

⏱️ ~7 minutes · Use Python. Solutions in solutions.html.

Goal

Package logic into reusable functions that return values, then feed those values into decisions.

Tasks

  1. Write a function is_even(n) that returns True if n is even, False otherwise. (Hint: n % 2 == 0 is already a boolean — you can return it directly.) Test it: print(is_even(4))True, print(is_even(7))False.

  2. Write a function grade(score) that returns a letter:

    • "A" for 90+, "B" for 80–89, "C" for 70–79, otherwise "F". Test it with a few scores.
  3. Call your functions together:

    print("Score 85 is even:", is_even(85), "and gets a", grade(85))

Stretch (optional)

What you're practicing

Defining functions, parameters vs. arguments, return values, and composing functions — the anatomy behind every library call you'll make in AI (model.fit(...), np.mean(...)).

← Lab 2Lab 4 →