IntroductionIf 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
Falsecalled 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.
AndThe first comparison operator is
and. You can use
andto test in one statement
andanother statement are both true.
is_it_true = (3 * 4 > 10) and (5 + 5 >= 10) print(is_it_true)The code prints out
3 * 4 > 10and
5 + 5 >= 10are 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 > 10is not true, the code prints out
False. In order for an
andexpression to return
True, both statements on either side of
andmust 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
There can be any number of statements. Python is always going to look at what two things are on either side of
Trueif the are both true or
Falseif 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)
oroperator also compares the statements on either side of it. In the case of
orit evaluates if one statement or the other is true. If either one is, the entire expression will evaluate to
True. In order for an
orexpression 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
Truebecause even though
5 + 5 > 10is not true,
3 * 4 > 10is.
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
oronly requires that one statement be true for the entire expression to also be true.
notoperator checks if something is not true. If the expression that it is evaluating is not true,
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
notis evaluating is true, it returns
Combining ThemYou 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
Trueeven though there were false statements. Python compared what was on either side of the
oroperators first. Because
oronly requires one of the statements that it's evaluating to be true, each
andchecked if the statements on either side of it were true. The
orexpressions were on either side of the
and, and they were both true, so the
andand 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
(2 * 6 <= 10) and (32 / 8 >= 4)is false because
2 * 6 <= 10is false.
5 ** 2 < 25is false, but
Truewhen given a false statement. So, with a false statement on one side of the
orand a true one on the other,
Truealong with the entire expression.
ConclusionBoolean 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.
andmeans both must be true.
ormeans that one must be true.
notjust 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
- Python Introduction and Installation Guide
- Python Files and the Interpreter
- Experimenting With Numbers and Text In Python
- Python Variables
- Working With Number Variables In Python
- Python String Basics
- Advanced Python Strings
- Python Comments
- Python Lists
- Python List Methods
- Python Multidimensional Lists
- Python Tuples
- Python Boolean Operators
- Python If Statements
- Python While Loops
- Python For Loops
- Python Dictionaries
- Python Advanced Dictionaries
- Python Functions