Python Programming – Expressions
Expressions form a basic sentence in any programming language. To construct an expression, we need two basic entities, which are operand and operators. As described above, Python language provides a rich set of operators. Operators in Python language are symbols, which are used to perform not only arithmetic computations such as addition, subtraction, multiplication, or division but also logical computations. The operators specify the type of operation that needs to be applied. The values that are operated on by the operators are called operands.
The combination of variables and constants along with Python operators create expressions. In other words, operators take more than one expression or operand to perform arithmetic and logical computations on it. The expressions are evaluated by using the operator precedence rules, which determine the order in which the operators in an expression are evaluated.
- Python Programming – Expressions
- Python Programming – Operators
- Python Programming – Logical/Boolean Operators
The precedence and associativity of operators are described in the following sections.
Python Operator Precedence
The precedence of an operator tells the compiler the order in which the operators should be evaluated. In case two operators with the same precedence are part of an expression, then associativity is taken into consideration. The associativity of an operator tells the compiler from which side (either left to right or vice versa) the expression should be resolved.
The combination of values, variables, operators, and function calls is termed as an expression. Python interpreter can evaluate a valid expression. For example, consider the following Code 3.8.
Code: 3.8.
20-50 |
Here 20- 50 is an expression. There can be more than one operator in an expression. To evaluate these types of expressions there is a rule of precedence in Python. It guides the order in which operations are carried out. For example, multiplication has higher precedence than subtraction as shown in the following Code 3.9.
Code 3.9. (Interactive mode)
>>>print(20-10*5) >>>-30 |
But we can change this order using parentheses ( ) as it has higher precedence as shown in Code-3.10.
Code,: ‘3.10.
>>>print((20-10)*5) >>>50 |
The precedence of operators in Python is listed in Table 3.9. It is in descending order, the upper group has higher precedence than the lower ones.
Operators | Meaning |
0 | Parentheses |
** | Exponent |
+ , ∼ | Unary plus, Unary minus, Bitwise NOT |
*, /, //, % | Multiplication, Division, Floor division, Modulus |
+, – | Addition, Subtraction |
<<,>> | Bitwise shift operators |
& | Bitwise AND |
∧ | Bitwise XOR |
| | Bitwise OR |
==, !=, >, >=, <, <=, is, is not, in, not in | Comparisons, Identity, Membership operators |
Not | Logical NOT |
And | Logical AND |
or | Logical OR |
Associativity
We can see in the above table that more than one operator exists in the same group. These operators have the same precedence. Associativity is the order in which an expression is evaluated that has multiple operators of the same precedence. Almost all the operators have left- to-right associativity except the exponentiation which is right-to-left associative. For example, multiplication and floor division have the same precedence. Hence, if both of them are present in an expression, the left one evaluates first. The Code 3.13. represents how an expression is evaluated in Python.
Code: 3.13. Illustration of expression evaluation considering associativity, (interactive mode)
>>>print(10*2//3) >>>6.666666666666667 >>>print(10*(2//3)) >>>0 |
Exponent operator ** has right-to-left associativity in Python and illustration is given in Code 3.11.
Code: 3.11. Illustration of associativity of exponent operator
>>>2 ** 2 ** 3 >>>256 >>>(2 ** 2) ** 3 >>>64 |
We can see that 2 ** 3 ** 2 is equivalent to 2 ** (3 ** 2).
Non Associative Operators
Some operators such as comparison operators and assignment operators do not possess associativity in Python. There are separate rules for sequences of this kind of operator and cannot be expressed as associativity. For example, a<b<c neither means (a<b) <c nor a< (b<c), a<b<cis equivalent to a<b and b<c, and is evaluates from left-to-right. Furthermore, while chaining of assignments like a= b = c is perfectly valid, a= b += 2 will result into error. An illustration is given in Fig.3.1.