3.16 Boolean Operators and, or and not

The conditional operators >, <, >=, <=, == and != can be used to form simple conditions such as grade >= 60. To form more complex conditions that combine simple conditions, use the and, or and not Boolean operators.

Boolean Operator and

To ensure that two conditions are both True before executing a control statement’s suite, use the Boolean and operator to combine the conditions. The following code defines two variables, then tests a condition that’s True if and only if both simple conditions are True—if either (or both) of the simple conditions is False, the entire and expression is False:

In [1]: gender = 'Female'

In [2]: age = 70

In [3]: if gender == 'Female' and age >= 65:
   ...:     print('Senior female')
   ...:
Senior female

The if statement has two simple conditions:

  • gender == 'Female' determines whether a person is a female and

  • age >= 65 determines whether that person is a senior citizen.

The simple condition to the left of the and operator evaluates first because == has higher precedence than and. If necessary, the simple condition to the right of and evaluates next, because >= has higher precedence than and. (We’ll discuss shortly why the right side of an and operator evaluates only if the left side is True.) The entire if statement condition is True if and only if both of the simple conditions are True. The combined condition can be made clearer by adding redundant (unnecessary) parentheses

(gender == 'Female') and (age >= 65)

The table below summarizes the and operator by showing all four possible combinations of False and True values for expression1 and expression2—such tables are called truth tables:

A truth table shows the and operator by showing all four possible combinations of False and True values for expression 1 and expression 2.

Boolean Operator or

Use the Boolean or operator to test whether one or both of two conditions are True. The following code tests a condition that’s True if either or both simple conditions are True—the entire condition is False only if both simple conditions are False:

In [4]: semester_average = 83

In [5]: final_exam = 95

In [6]: if semester_average >= 90 or final_exam >= 90:
   ...:    print('Student gets an A')
   ...:
Student gets an A

Snippet [6] also contains two simple conditions:

  • semester_average >= 90 determines whether a student’s average was an A (90 or above) during the semester, and

  • final_exam >= 90 determines whether a student’s final-exam grade was an A.

The truth table below summarizes the Boolean or operator. Operator and has higher precedence than or.

A truth table shows the Boolean or operator.

Improving Performance with Short-Circuit Evaluation

Python stops evaluating an and expression as soon as it knows whether the entire condition is False. Similarly, Python stops evaluating an or expression as soon as it knows whether the entire condition is True. This is called short-circuit evaluation. So the condition

gender == 'Female' and age >= 65

stops evaluating immediately if gender is not equal to 'Female' because the entire expression must be False. If gender is equal to 'Female', execution continues, because the entire expression will be True if the age is greater than or equal to 65.

Similarly, the condition

semester_average >= 90 or final_exam >= 90

stops evaluating immediately if semester_average is greater than or equal to 90 because the entire expression must be True. If semester_average is less than 90, execution continues, because the expression could still be True if the final_exam is greater than or equal to 90.

In operator expressions that use and, make the condition that’s more likely to be False the leftmost condition. In or operator expressions, make the condition that’s more likely to be True the leftmost condition. These can reduce a program’s execution time.

Boolean Operator not

The Boolean not operator “reverses” the meaning of a condition—True becomes False and False becomes True. This is a unary operator—it has only one operand. You place the not operator before a condition to choose a path of execution if the original condition (without the not operator) is False, such as in the following code:

In [7]: grade = 87

In [8]: if not grade == -1:
   ...:    print('The next grade is', grade)
   ...:
The next grade is 87

Often, you can avoid using not by expressing the condition in a more “natural” or convenient manner. For example, the preceding if statement can also be written as follows:

In [9]: if grade != -1:
   ...:    print('The next grade is', grade)
   ...:
The next grade is 87

The truth table below summarizes the not operator.

A truth table shows the not operator with 2 columns left to right expression and not expression. Row 1 False, True. Row 2 True, False.

The following table shows the precedence and grouping of the operators introduced so far, from top to bottom, in decreasing order of precedence.

A table shows the precedence and grouping of the operators from top to bottom, in decreasing order of precedence.

tick mark Self Check

  1. (IPython Session) Assume that i = 1, j = 2, k = 3 and m = 2. What does each of the following conditions display?

    1. (i >= 1) and (j < 4)

    2. (m <= 99) and (k < m)

    3. (j >= i) or (k == m)

    4. (k + m < j) or (3 - j >= k)

    5. not (k > m)
      Answer:

    In [1]: i = 1
    
    In [2]: j = 2
    
    In [3]: k = 3
    
    In [4]: m = 2
    
    In [5]: (i >= 1) and (j < 4)
    Out[5]: True
    
    In [6]: (m <= 99) and (k < m)
    Out[6]: False
    
    In [7]: (j >= i) or (k == m)
    Out[7]: True
    
    In [8]: (k + m < j) or (3 - j >= k)
    Out[8]: False
    
    In [9]: not (k > m)
    Out[9]: False