def is_even(n):
return n % 2 == 0
print(is_even(8))
print(is_even(7))
print(is_even(0))
#source code --> clcoding.com
Code Explanation:
Define the Function:
def is_even(n): → This defines a function named is_even that takes a single parameter n.
Check if the Number is Even:
return n % 2 == 0 →
% is the modulus operator, which gives the remainder when n is divided by 2.
If n % 2 == 0, it means n is even, so the function returns True.
Otherwise, it returns False.
Calling the Function:
is_even(8) → Since 8 % 2 == 0, it returns True.
is_even(7) → Since 7 % 2 == 1, it returns False.
is_even(0) → Since 0 % 2 == 0, it returns True.
Example Output
True
False
True
0 Comments:
Post a Comment