Shorten!

Report a typo

Which of the following code snippets is the best way to declare a function that determines the parity of a number?

1)

def is_even(number):
    if number % 2 == 0:
        return True
    else:
        return False

2)

def is_even(number):
    if number % 2 == 0:
        return True
    return False

3)

def is_even(number):
    return True if number % 2 == 0 else False

4)

def is_even(number):
    return number % 2 == 0

In all cases, the function is_even() does exactly the same: checks if a number is even or not and returns a boolean value (either True or False). The question is aimed at increasing code simplicity!

Select one option from the list
___

Create a free account to access the full topic