Before, we've seen mathematical operators like +, - and %. We know about relational operators like ==, >=, <=, >, < and so on, that compare two values to each other. Now, we learn how to string together many of these conditions to create compound condition statements.
Introduction
First, we'll discuss how the three basic logical operators work: not, or and and.
not
The not operator does not change the way control flows, which is why we discuss it first. It changes a Truth-y value to a False-y one, and vice-versa.
The or operator returns the first Truth-y value it finds, or β in case no Truth-y value is found β the last False-y value it finds.
>>>1or21>>>0or11>>>0orNoneNone>>>0orNoneor11
and
The and operator returns the first False-y value it finds, or β in case no False-y value is found β the last Truth-y value it finds.
Notice how and and or behave complimentarily from each other!
Short-Circuiting
So what is so special about and and or? Here's the special thing β when they find their first False-y and first Truth-y value respectively, they return that value and do not keep reading that sentence!
Let's take an example:
We know that 1/0 typically throws a Division by Zero Error. However, this statement does not do so! Instead, it finds the first Truth-y value β 1 β and returns it, not even reading the 1/0 at the end of the expression.