Skip to main content
Engineering LibreTexts

19.4: Boolean Functions

  • Page ID
  • Functions can return booleans, which is often convenient for hiding complicated tests inside functions. For example:

    def is_divisible(x, y):
        if x % y == 0:
            return True
            return False

    It is common to give boolean functions names that sound like yes/no questions; is_divisible returns either True or False to indicate whether x is divisible by y.

    Here is an example:

    >>>   is_divisible(6, 4)
    >>>   is_divisible(6, 3)

    The result of the == operator is a boolean, so we can write the function more concisely by returning it directly:

    def is_divisible(x, y):
        return x % y == 0

    Boolean functions are often used in conditional statements:

    if is_divisible(x, y):
        print 'x is divisible by y'

    It might be tempting to write something like:

    if is_divisible(x, y) == True:
        print 'x is divisible by y'

    But the extra comparison is unnecessary.

    Exercise \(\PageIndex{1}\)

    Write a function is_between(x, y, z) that returns True if xyz or False otherwise