Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

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

Download

Sample Code (.zip)

4.6 KB

Download

Course Slides (.pdf)

1.7 MB

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.

01:50 I hope you enjoyed this course!

Become a Member to join the conversation.