Using the Python or Operator (Summary)
You now know how the Python or
operator works as well as how to use it for solving some common programming problems in Python. In addition, you’ve learned a little bit about Boolean logic, as well as some of its main concepts in Python.
Now that you know the basics of the Python or
operator, you’ll be able to:
- Use the Python
or
operator in Boolean and non-Boolean contexts - Solve several kind of programming problems by effectively using the Python
or
operator - Write better and more Pythonic code by taking advantage of the somewhat special features of
or
in Python - Read and better understand other people’s code when they make use of the Python
or
operator
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00
Let’s review what we’ve learned about the Python or
operator. First, it truly is a Boolean or
operator. It evaluates two Boolean expressions and if at least one of them is True
, the result of the or
is True
.
00:17
Otherwise, if both expressions are False
, then the result of the or
operation is False
. But its use in Python has been expanded to allow Python to return objects so that the expression x or y
will return x
if x
is evaluated to be True
and otherwise it will return y
.
00:40
It does this following a predefined set of rules to determine the truth value of an object. Empty strings, empty collections, things like that all evaluate to be False
for example.
00:53
Its behavior is also to stop evaluating operands once it knows the result of the entire operation. Starting from left to right, once it finds something that’s True
, it knows the result of the entire expression and returns it.
01:09
If it gets all the way to the last expression and it hasn’t found something that’s True
, then the result has to be whatever that last expression evaluates to.
01:21
So hopefully, now you can use the or
operator in both Boolean and non-Boolean contexts. There are several problems now that you can solve using the or
operator and now you can write better code taking advantage of the special features or
provides—or that or
is provided by in Python—and now you’ll be able to read and understand other people’s code much better when they make use of the Python or
operator.
Become a Member to join the conversation.
wggustafson on June 24, 2022
A+. Watch and learn from this course if you’re not totally up to speed or a bit rusty on booleans.