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.
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)
But we can change this order using parentheses ( ) as it has higher precedence as shown in Code-3.10.
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.
|+ , ∼||Unary plus, Unary minus, Bitwise NOT|
|*, /, //, %||Multiplication, Division, Floor division, Modulus|
|+, –||Addition, Subtraction|
|<<,>>||Bitwise shift operators|
|==, !=, >, >=, <, <=, is, is not, in, not in||Comparisons, Identity, Membership operators|
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)
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
>>>(2 ** 2) ** 3
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.