1.3.3 Expressions and the Ternary Operator
1.3.3 Expressions and the Ternary Operator jmk649Expressions
Expressions should be familiar to us all and for Python, these mathematical symbols are referred to as binary operators. In Python, a binary operator is an operator that works with two values, called operands. For example, in the expression 4 + 2, the + symbol is a binary operator that takes the two operands 4 and 2 and produces the result 6.
Binary operators can be used to form more complex expressions that evaluate to different kinds of results. For instance, arithmetic expressions evaluate to numbers, while boolean expressions evaluate to either True or False.
Here’s an example of an arithmetic expression that uses subtraction (-) and multiplication (*):
x = 25 – 2 * 3
All Python operators are organized into different precedence classes, determining in which order the operators are applied when the expression is evaluated unless parentheses are used to explicitly change the order of evaluation. This operator precedence table shows the classes from lowest to highest precedence. The operator * for multiplication has a higher precedence than the – operator for subtraction, so the multiplication will be performed first and the result of the overall expression assigned to variable x is 19.
Here is an example for a boolean expression:
x = y > 12 and z == 3
The boolean expression (y > 12 and z == 3) on the right side of the assignment operator contains three binary operators. The two comparison operators are: > and == and these take two numbers and return a boolean value. The logical and operator takes two boolean values and returns a new boolean (True only if both input values are True, False otherwise). The precedence of and is lower than that of the two comparison operators, so the and will be evaluated last. If y has the value 6, and z the value 3, the value assigned to variable x by this expression will be False because the comparison on the left side of the and evaluates to False.
Another way of writing the above expression that highlights the operators can be done using ( ):
x = (y > 12) and (z == 3)
Ternary Operator
In addition to these binary operators, Python has a ternary operator. This is an operator that takes three operands as input in the format:
x if c else y
x, y, and c here are the three operands, and the if and else are the familiar decision operators demarcating the operands. While x and y can be values or expressions, the condition c needs to be a boolean value or expression. The operator (if and else) looks at the condition c and if c is True it evaluates to x, else it evaluates to y. So for example, in the following line of code:
p = 1 if x > 12 else 0
variable p will be assigned the value 1 if x is larger than 12, else p will be assigned the value 0. The ternary if-else operator is very similar to what we can do with an if or if-else statement, but with less code. For example, It could be written as:
p = 1
if x > 12:
p = 0The “x if c else y” operator is an example of a language construct that does not add anything principally new to the language, but enables writing things more compactly or more elegantly. That’s why such constructs are often called syntactic sugar. The nice thing about “x if c else y” is that in contrast to the if-else statement, it is an operator that evaluates to a value, and can be embedded directly within more complex expressions. For example, this one line uses the operator twice:
newValue = 25 + (10 if oldValue < 20 else 44) / 100 + (5 if useOffset else 0)Using an if-else statement for this expression would have required at least five lines of code- which is perfectly ok! The ternary construct works well if the result can be one of the two values. If you have more than two possibilities, you will need to utilize a different decision structure such as an if-elif-else, an object literal, or a switch case structure.