Python Tip #299 (of 365): Return Boolean expressions directly. Do this: def is_e...

@treyhunner@mastodon.social

Python Tip #299 (of 365):

Return Boolean expressions directly.

Do this:
def is_even(n):
return n % 2 == 0

Not this:
def is_even(n):
if n % 2 == 0:
return True
else:
return False

That "n % 2 == 0" condition will have a value of either True or False... which is also what we then return.

So there's no need for an "if" statement and returning the result of the boolean expression directly is clearer.

๐Ÿงต (1/3)

#Python #DailyPythonTip