In this lesson, we introduce four concepts.
First, we describe keywords that behave as operators.
Second, we describe a special evaluation technique
used by Boolean operators called short-circuit evaluation.
Third, we introduce a new expression called a unary expression.
Finally, we described the operation order Python
uses for computations that contain more than one operator.
Recall that operator tokens,
such as plus and less than,
are used in binary expressions to apply an operator function to two operands.
There are 19 operator tokens.
Some Python keywords can also be used as operators.
Let's look more closely at all of the keywords.
You've seen that some keywords,
such as from, import,
and if, are used as delimiters to punctuate statements.
In fact, 26 of 33 keywords
are used as delimiters to separate different parts of statements.
You have also seen that some keywords,
such as true and false,
are used as literals that are bound to specific objects.
In fact, none is also used as a keyword literal,
and it is bound to an object whose type is NoneType,
which is returned by function calls that don't really need to return a meaningful object.
The other four keywords,
and, is, not, and or,
are used as operators,
and the keyword in can actually be used as an operator or delimiter depending on context.
I will revise the syntax diagram for binary operator by
adding the keyword operators that can be used as binary operators.
Notice that in addition to and,
in, is and or,
there are two keyword operator pairs: not in and is not.
They can each be used as a single binary operator.
I've added some new binary operators,
but the semantic rule for binary expressions does not change.
For example, I will apply
the binary expression semantics to the expression false and unbound.
In step one, the left expression is the keyword literal false,
which evaluates to the Boolean object false.
In step two, the operator is and.
Well, the type of the first operand is Boolean since the operand is false.
Normally, both the operator and
operand type are needed to identify the operator function.
However, when the operator is and,
the type of the first operand is actually ignored.
The and operator always invokes
the same operator function regardless of the first operand type.
In step three, the interpreter applies this operator function and.
The operator function and uses a technique called short-circuit evaluation,
where the result object may not depend on the second operand.
Here are the semantics of the and operator function.
One, if the first operand is interpreted as false,
the first operand is the result object.
Two, otherwise, evaluate the second expression as the result object.
In step one, the first operand is false,
so this first operand object false is returned.
Step two is skipped,
so the second expression is not even evaluated.
If the second expression was evaluated,
it would have displayed a semantic name error,
since I did not bind to the identifier unbound to any object.
The and operator function has another interesting property.
In step one, the first operand is the empty string,
which you may recall is interpreted as false in an if statement condition.
The and operator function semantics interpret non-Boolean objects the same way.
So, the empty string is interpreted as false.
Therefore, the empty string is the result object.
If I had used zero or 0.0 instead of the empty string,
the expression would evaluate to zero or 0.0 respectively.
Again, step two is skipped,
so the second expression is not evaluated and no semantic name error is displayed.
If the first operand is not interpreted as false,
the second expression is evaluated.
For example, if I evaluate hello and goodbye,
the interpreter returns goodbye.
In step one, the first operand is the string hello,
which is interpreted as true.
Since it is not interpreted as false,
the second step is applied.
In step two, the second expression evaluates to the string goodbye,
which is used as the result object.
The or operator function also uses short-circuit semantics,
if the first operand is interpreted as true,
the first operand is the result object.
Otherwise, evaluate the second expression as the result object.
Notice that neither of the operator token tilde nor the keyword operator not,
by itself, can be used as a binary operator.
Instead, both are unary operators that act on a single operand.
Here is the syntax diagram for a unary expression.
Here's a more general syntax diagram for expression that now includes unary expressions.
The plus and minus operator tokens can be used either as binary or unary operators.
The semantics of unary expressions is similar to the semantics of binary expressions,
except that there is only one operand to evaluate.
For example, I will apply the unary expression semantics to minus 27.
In step one, the expression is the literal integer 27,
which evaluates to the int object 27.
In step two, the unary operator minus and
the type int are used to identify the numerical negation function,
which reverses the sign of its integer operand object.
In step three, the numerical negation function is applied to the int object
27 to obtain the int object negative 27.
Notice that negative 27 is not a literal integer.
It is a unary expression that applies
the minus operator to an integer object to obtain a result object.
If more than one operator appears in an expression,
the Python interpreter must select in order for the operations.
For example, in the expression three plus four star five,
there are two operators: plus and star.
If the Python interpreter applies these operators in order from left to right,
then three plus four is seven and seven times five is 35.
Thirty-five is not the correct answer.
Twenty-three is.
Python applies the star operator before applying the plus operator,
analogist to the order of operations in mathematics.
Python has an operator precedence table that defines the order that should
be used to apply multiple operators in the same expression.
Operators that are below in the table have higher precedence,
and are applied before operations that are above in the table.
For example, star is applied before plus,
since it is below plus in the table.
That is why three plus four star five evaluates to 23 instead of 35.
You should recognize many of the operators in
this table such as the operator tokens plus,
times, and less than and the keyword operators and and or.
In this lesson, we described keywords that behave as operators.
We also described a special evaluation technique used
by Boolean operators called short-circuit evaluation,
a new kind of expression called the unary expression,
and what operation order Python uses for
computations that contain more than one operator.