Lab · School of Computer Science & Technology
Lab 3 — Functions
Goal
Package logic into reusable functions that return values, then feed those values into decisions.
Tasks
Write a function
is_even(n)that returnsTrueifnis even,Falseotherwise. (Hint:n % 2 == 0is already a boolean — you can return it directly.) Test it:print(is_even(4))→True,print(is_even(7))→False.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.
Call your functions together:
print("Score 85 is even:", is_even(85), "and gets a", grade(85))
Stretch (optional)
- Add a parameter to
gradewith a default value, e.g.grade(score, passing=70), and return"PASS"/"FAIL". - Demonstrate scope: create a variable
inside
gradeand try to print it outside the function. What error do you get, and why?
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(...)).