leafleafleafDocy banner shape 01Docy banner shape 02Man illustrationFlower illustration

AND OR NOT (Boolean) in Python

Estimated reading: 4 minutes 63 views

Just as we can use arithmetic operators (+, −, *, /, etc.) to combine numbers, we can use Boolean operators to combine Boolean values. The 3 main Boolean operators in Python are ANDOR, and NOT. When two or more Boolean values ​​are combined with a Boolean operator, the result is also a Boolean value.

AND OPERATOR

“AND” means “and”, use this operator to determine whether two boolean values ​​are True. When both booleans are True, the result is True; otherwise, the result is False.

>>> True and True
True
>>> True and False
False
>>> False and False
False


Let’s look at an example. We use the variable isAfterSchool to indicate “whether school is over”, and the variable isFinishHomework to indicate “whether the homework has been completed”. Only when “school has been dismissed” and “homework completed” can we go out to play.

Then, we set the variable isAfterSchool to True, indicating that school has been dismissed; and set the variable isFinishHomework to False, indicating that the homework has not been completed. Through the results of the expressions isAfterSchool and isFinishHomework, let’s see if we can go out and play.

> >>> isAfterSchool=True
>>> isFinishHomework=False
>>> isAfterSchool and isFinishHomework
False

The result is False, which means that both conditions are not met, so you can’t go out and play.

When the job is completed, we change the variable isFinishHomework to True, and look at the results of the expressions isAfterSchool and isFinishHomework:

> >>> isAfterSchool=True
>>> isFinishHomework=True
>>> isAfterSchool and isFinishHomework
True

The result this time is True, which means that you have the conditions to go out and play.

or (or)

The Boolean operator or stands for “or”, and can be used to determine whether one of two Boolean values ​​is True. The result is True when at least one of the two Boolean values ​​is True; otherwise, the result is False.

> >>> True or True
True
>>> True or False
True
>>> False or False
False


Let’s still look at the example given earlier. This time we have modified the conditions. As long as one of the items “has been dismissed from school” or “homework completed” is satisfied, you can go out to play.

We set the variable isAfterSchool to True, indicating that school has been dismissed; the variable isFinishHomework is set to False, indicating that the homework has not been completed. Through the result of the expression isAfterSchool or isFinishHomework, let’s see if we can go out and play.

> >>> isAfterSchool=True
>>> isFinishHomework=False
>>> isAfterSchool or isFinishHomework
True

The result is True, because at least one of the two conditions is met, so go out and play. 

not (not)

not means “not”, use this operator to invert a value, convert False to True, or convert True to False.

> >>> not True
False
>>> not False
True


Still looking at the example given earlier, suppose the variable isFinishHomework has been set to True, indicating that the job has been completed. Suddenly found that there is a job missing, then we can use the not operator to modify the isFinishHomework variable.

> >>> isFinishHomework=True
>>> not isFinishHomework
False

Combining Boolean Operators

Things get interesting when we combine boolean operators. For example, if today is a weekend, you can go out to play; if it is not a weekend, you can go out to play after school and homework are completed.

> >>> isWeekend=False
>>> isAfterSchool=True
>>> isFinishHomework=True
>>> isWeekend or (not isWeekend and isAfterSchool and isFinishHomework)
True

In the example above, we see that today is not the weekend, school is over and homework is done, and it turns out it’s time to go out and play. We put not isWeekend and isAfterSchool and isFinishHomework in parentheses to ensure that these parts are executed together.

Leave a Comment

CONTENTS