Arithmetic Operators
There are around 7 arithmetic operators available in python. These are called as binary operators because they act on two operators.
a = 13, b = 5
Operator | Meaning | Example | Result |
---|---|---|---|
+ | Addition Operator | a+b | 18 |
– | Subtraction Operator | a-b | 8 |
* | Multiplication Operator | a*b | 65 |
/ | Division Operator | a/b | 2.6 |
% | Modulus Operator | a%b | 3 |
** | Exponent Operator | a**b | 371293 |
// | Integer Division Operator | a//b | 2 |
Operator Precedence
When there are several operators in an equation, it is evaluated based on operator precedence. For example lets take an expression (1+2)*3*2//2+3
First preference would be given to operands inside parenthesis – (1+2) = 3
Second preference would be given to Exponentiation i.e ** – 3**2 = 9
Next Multiplication, Division, Modulus and floor divisions are given equal priority – 3*9//2+3 = 27//2+3 = 13+3
Next Addition and subtraction are preferred – 13 + 3 = 16
Final preference is given to assignment operator. 16 is assigned to an operand on LHS of ‘=’