Operator precedence defines the order in which operators are applied in an expression in the absence of parentheses

Consider the expression 4 + 1 * 3 * 5
. Without parentheses, it can be evaluated in two ways:
(4 + 1) * 3 * 5
(addition first, then multiplication)4 + (1 * 3 * 5)
(multiplication first, then addition)
By Python’s operator precedence rules, multiplication is performed before addition. Therefore:
1 * 3 * 5
is evaluated first, resulting in15
.- Then,
4 + 15
is evaluated, resulting in19
.
So, 4 + 1 * 3 * 5
evaluates to 19
in Python due to the higher precedence of multiplication over addition.
Operator associativity
Operator associativity defines the order in which operators with the same precedence are applied when they appear in a sequence. There are two types of associativity:
- Left-to-Right Associativity: Operators are evaluated from left to right within an expression.
- Right-to-Left Associativity: Operators are evaluated from right to left within an expression.
