Here's a simple function in Python that checks if a number is even:
def is_even(n):
return n % 2 == 0
Explanation:
- The function
is_even
takes an integern
as input. - It uses the modulus operator
%
to check the remainder ofn
when divided by 2. - If the remainder is 0, it returns
True
(indicating thatn
is even); otherwise, it returnsFalse
.
Example Usage:
print(is_even(4)) # Outputs: True
print(is_even(7)) # Outputs: False