Boolean FunctionsΒΆ




(ch06_boolfun1)

The name of this function is isDivisible. It is common to give boolean functions names that sound like yes/no questions. isDivisible returns either True or False to indicate whether the x is or is not divisible by y.

We can make the function more concise by taking advantage of the fact that the condition of the if statement is itself a boolean expression. We can return it directly, avoiding the if statement altogether:

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

Here is the same function in codelens. When we evaluate the if statement in the main part of the program, the evaluation of the boolean expression causes a call to the isDivisible function.

(ch06_boolcodelens)

Check your understanding

select-8-1: What is a Boolean function?




select-8-2: Is the following statement legal in Python (assuming x, y and z are defined to be numbers)?

return x + y < z



Next Section - Exercises