Python Boolean Operators

Introduction

If you’ve been following along, you’re probably tired of hearing about lists right about now. Well, this guide has nothing to do with lists! Remember back when you first leaned about variables; how there was one that just held True or False called a Boolean? There hasn’t been a guide on them because Booleans are actually that simple. However, there are symbols called Boolean Operators that are used to evaluate whether a statement is true or false. They compare statements and return either true or false. It sounds simple, and in a way it is, but it can get more complex when more comparisons get added.

And

The first comparison operator is and. You can use and to test in one statement and another statement are both true.

is_it_true = (3 * 4 > 10) and (5 + 5 >= 10)
print(is_it_true)

The code prints out True because both 3 * 4 > 10 and 5 + 5 >= 10 are true.



Try one out where one of the statements is true and the other is false.

is_it_true = (3 * 4 > 10) and (5 + 5 > 10)
print(is_it_true)

Because 5 + 5 > 10 is not true, the code prints out False. In order for an and expression to return True, both statements on either side of and must be true.

You can have multiple Boolean Operators in one statement.

is_it_true = (3 * 4 > 10) and (5 + 5 >= 10) and (4 * 4 > 15)
print(is_it_true)

It doesn’t matter that there are multiple statements. Each statement must be true in order for the whole to evaluate to True.

There can be any number of statements. Python is always going to look at what two things are on either side of and and return True if the are both true or False if any or all of them are false.

is_it_true = (3 * 4 > 10) and (5 + 5 >= 10) and (4 * 4 > 15) and (5 + 4 < 10)
print(is_it_true)


Or

The or operator also compares the statements on either side of it. In the case of or it evaluates if one statement or the other is true. If either one is, the entire expression will evaluate to True. In order for an or expression to return False, both statements must be false.

is_it_true = (3 * 4 > 10) or (5 + 5 > 10)
print(is_it_true)

The expression is True because even though 5 + 5 > 10 is not true, 3 * 4 > 10 is.

Like with and, these can also be combined.

is_it_true = (3 * 4 < 10) or (5 + 5 > 10) or (4 * 4 > 15) or (5 + 4 > 10)
print(is_it_true)

Even though only one of the statements is true, the expression as a whole is true. Using or only requires that one statement be true for the entire expression to also be true.

Not

The not operator checks if something is not true. If the expression that it is evaluating is not true, not will evaluate True. That might seem weird, so here's an example.

is_it_true = not (3 * 4 > 10)
print(is_it_true)

Since the statement that not is evaluating is true, it returns False.

Combining Them

You can combine different Boolean Operators into the same expression. Using them in conjunction with one another allows you to create finer grained control over the logic in your programming, but it also adds a new degree of complexity.

is_it_true = (3 * 4 > 10) or (5 + 5 > 10) and (4 * 4 > 15) or (5 + 4 > 10)
print(is_it_true)


It evaluated True even though there were false statements. Python compared what was on either side of the or operators first. Because or only requires one of the statements that it's evaluating to be true, each or evaluated to True. Then, and checked if the statements on either side of it were true. The or expressions were on either side of the and, and they were both true, so the and and the expression as a whole are also true.

is_it_true = (2 * 6 <= 10) and (32 / 8 >= 4) or not (5 ** 2 < 25)
print(is_it_true)

Again, this one came back True. (2 * 6 <= 10) and (32 / 8 >= 4) is false because 2 * 6 <= 10 is false. 5 ** 2 < 25 is false, but not evaluates True when given a false statement. So, with a false statement on one side of the or and a true one on the other, or will evaluate True along with the entire expression.

Conclusion

Boolean Operators operate based on logic. That's probably the best thing to keep in mind when dealing with them. Think through exactly how statements are compared with on another in a logical procedure.

You can also think of the operators in very simple terms. and means both must be true. or means that one must be true. not just evaluates to the opposite.

It's good practice to come up with as complex of these expressions as you can and try to figure out how they will ultimately evaluate. It takes some getting used to, but the more that you do it, the more familiar you will become with Boolean Operators and Boolean Logic.

Table of Contents

  1. Python Introduction and Installation Guide
  2. Python Files and the Interpreter
  3. Experimenting With Numbers and Text In Python
  4. Python Variables
  5. Working With Number Variables In Python
  6. Python String Basics
  7. Advanced Python Strings
  8. Python Comments
  9. Python Lists
  10. Python List Methods
  11. Python Multidimensional Lists
  12. Python Tuples
  13. Python Boolean Operators
  14. Python If Statements
  15. Python While Loops
  16. Python For Loops
  17. Python Dictionaries
  18. Python Advanced Dictionaries
  19. Python Functions


Comments and Discussions
Linux Forum