CodeNest

Functions

A function packages code so you can reuse it. Define one with def, give it parameters, and hand a result back with return:

def area(width, height):
    return width * height

print(area(3, 4))   # 12

Parameters can have defaults: def greet(name="friend").

A function that never hits a return gives back None.

Try it

Implement is_even(n) so it returns True for even numbers and False otherwise. The loop below it is already written; the expected output is:

4 is even
7 is odd
10 is even

Hint: a number is even when n % 2 == 0.

Python in-browser · Python 3.13
Next
Loading editor…

Press Ctrl/ + Enter or click Run to see the output here.