What are the different arithmetic operators used in Python?
What do you mean by precedence of operators? Give examples.
Python
Computer Science Engineering
782
Dashboard
The different arithmetic operators used in Python are: *,/, +,-, %,**,and //.
Precedence of arithmetic operators in decreasing order
Operator precedence determines which operator is performed first in an expression with more than one operator with different precedence.
Precedence of operators:
Python will always evaluate the arithmetic operators first (** is highest, then multiplication/division, then addition/subtraction). Next comes the relational operators. Finally, the logical operators are done last. This means that the expression x*5 >= 10 and y-6 <= 20
will be evaluated so as to first perform the arithmetic and then check the relationships.
The following table summarizes the operator precedence from highest to lowest.
Level |
Category |
Operators |
---|---|---|
7(high) |
exponent |
** |
6 |
multiplication |
*,/,//,% |
5 |
addition |
+,- |
4 |
relational |
==,!=,<=,>=,>,< |
3 |
logical |
not |
2 |
logical |
and |
1(low) |
logical |
or |