Is Odd Or Even With Python

How To Check If An Integer Is Odd (Or Even) With Python

Just for fun, how many ways can we determine if an integer, just an integer, (not a float) is odd or even?

Is Odd

Rules: Must return True if odd and False if even.

def is_odd_1(x):
    return x % 2 != 0

def is_odd_2(x):
    return ((-1)\*\*x) != 1

def is_odd_3(x):
    return x - 2 \* (x // 2) == 1

def is_odd_4(x):
    return (x & 1) == 1

def is_odd_5(x):
    return x / 2 != x // 2

Is Odd Or Even

Rules: Must return odd if odd and even if even.

def is_odd_even_1(x):
    return 'odd' if x % 2 else 'even'

def is_odd_even_2(x):
    return 'odd' if x & 1 else 'even'

def is_odd_even_3(x):
    return x % 2 and 'odd' or 'even'

Check All Functions

def check():
    nums = [10, 11]
    is_odds = [is_odd_1, is_odd_2, is_odd_3, is_odd_4, is_odd_5, is_odd_5]
    is_odd_evens = [is_odd_even_1, is_odd_even_2, is_odd_even_3]

    for i, num in enumerate(nums):
        if i == 0:
            print('\nExpected: False')
            print('\nExpected: True')

        for is_odd in is_odds:
            r = is_odd(num)
            print(is_odd.__name__, r)

        if i == 1:
            print('\nExpected: even')
            print('\nExpected: odd')

        for is_odd_even in is_odd_evens:
            r = is_odd_even(num)
            print(is_odd_even.__name__, r)

Thanks for reading. x