BOOLEAN OPERATORS AND, OR AND NOT IN PYTHON PROGRAMMING

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 parentheses

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

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.

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 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 techniques can reduce a program’s execution time.

Boolean Operator not

The Boolean operator not “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

*

Post a Comment (0)
Previous Post Next Post