We’ve just seen how we can use the
or operator with Boolean expressions to control things like
if statements and
while loops. However, because Python can also perform
or operations on things that aren’t Boolean expressions, there are some new opportunities that we have for writing code to take advantage of that.
One is just simply the assignment of variables to a possibility of values in the event that one or more values might not exist. So, for example, suppose that I have two variables
b, and normally I want something to be assigned to
a, but in the event
a doesn’t contain anything meaningful, I want it to be assigned to
That’s one of the ways that you’re going to see in these next examples of how we use the word
or. The first possibility might be what we’re ideally looking for and the second one provides a backup value in the event that the first one doesn’t contain something meaningful for us to use.
Become a Member to join the conversation.