You can learn about Operators in Python Programs with Outputs helped you to understand the language better.

## Python Programming – Operators And Their Precedence and Associativity

All operators have two important properties called precedence and associativity. Both properties affect how operands are attached to operators. Precedence means the order in which two different kinds of operators should be applied in an expression. Associativity means the order in which the two operators with the same precedence should be applied in an expression.

- Note: When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence.

To understand how precedence works, consider the expression:

2 + 3 *4

Should it be interpreted as

(2+3) *4

(that is, 20), or rather is

2+(3 *4)

(that is, 14) the correct interpretation?

num3=int(input(“Enter number 3:”)) sum = numl + num2 + num3

print(“Three numbers are :” , numl, num2, num3)

print(“Sum is , sum)

As in normal arithmetic, multiplication and division in Python have equal importance and are performed before addition and subtraction. You can say multiplication and division have precedence over addition and subtraction. In the following expression:

2+3 *4

the multiplication is performed before addition since multiplication has precedence over addition. The result is 14. The multiplicative operators)*, /, / /, and %) have equal precedence with each other, and the additive operators (binary + and -) have equal precedence with each other. The multiplica¬tive operators have precedence over the

As in standard arithmetic, in Python, if the addition is to be performed first, parentheses can override the precedence rules. The expression:

(2+3) *4

evaluates to 20. Multiple sets of parentheses can be arranged and nested in any way that are acceptable in standard arithmetic.

All binary operators, except assignment, are left-associative. The assignment operator supports a technique known as chained assignment. The code:

w = x = y = z

can also be read right-to-left. First, y gets the value of z. Both w and x also get the value of z. In the case of precedence, parentheses can be used to override the natural associativity within an expression. The unary operators have higher precedence than the binary operators, and the unary operators are right-associative. This means the statements:

print(-3 + 2) print(-(3 + 2))

will display

-1

-5

- Note: In the case where operators have the same pre¬cedence, associativity (sometimes called binding) is used to determine the order in which operands are grouped with operators. Grouping occurs in either (right-to-left) or (left-to-right) order, depending on the operator.

A mixed-type expression is an expression contain¬ing operands of different types. When mathematical operations are performed with mixed operators in Python expression, Python uses rules and regulations to determine which operations to perform first, based on pre-determined precedence. The rule of operator precedence follows similar precedence as applied in most computer programming languages. Table 4.2 summarizes the operator precedence applied in Python programming.

Operator |
Description |

Lambda | Lambda expression |

Or | Boolean OR |

And | Boolean AND |

Not | Boolean NOT |

in, not in | Membership tests |

is is not | Identity tests |

<,<=,>,>=,!=,== | Comparisons |

I | Bitwise OR |

∧ | Bitwise XOR |

& | Bitwise AND |

<<,>> | Shifts |

+,- | Addition and subtraction |

*, /,% | Multiplication, division, the remainder |

+X, -X | Positive, negative |

~X | Bitwise not |

** | Exponentiation |

**Let’s Try**

Consider the following code containing mixed arithmetic expressions. What will be the final result and the final data type?

x = 7
y = 2 z = 5.0 k = 36.0 A = (x + y) / z B=k/z * x print(A) print(B) |
x = 4
y = 2 z = 4.0 k = 49.0 A = (x + y) / z B = k/z*x/2 print(A) print(B) |

**Let’s Try**

What will be the output produced by the following code?

A, B, C, D = 9.2,
2.0, 6, 24 print(A/4) print(A//4) print(B ** C) print(D//B) print(D%B) |
a, b = 10, 20
print(a= =b) print(a<b) print(a<=b) print(a!=b) |