What are Arithmetic Operators in Python?
Arithmetic Operators or Mathematical Operators are used to perform calculations on numeric data. Mathematical operations could include addition, subtraction, multiplication, division and other complex calculations on numeric data.
Arithmetical Operators in Python
Any programming language must provide operators and methods to handle numeric calculations. Mathematical Operators or Arithmetic operators facilitate the functionality of carrying out all the mathematical operations.
List of Arithmetic Operators
Arithmetic Operators | Example | Output | Explanation |
+ | Val=20 Val=Val+10 Print(Val) |
30 | 10 is added to the variable Val before printing using arithmetic operator ‘+’ |
– | Num = 10 Num = Num- 5 Print(Num) |
5 | 5 is subtracted from the variable Num before printing using arithmetic operator ‘-‘ |
* | Var = 10 Var = Var * 2 Print(Var) |
20 | Var having the value 10 is multiplied by 2 before printing using mathematical operator – ‘*’ |
/ | X=5 Y=2 Print(X/Y) |
2.5 | Before printing divides X by Y – resulting always in float type of value dipicting quotient part of the result after division |
// | Num=5 Print(Num//2) |
2 | Before printing divides Num by 2 – resulting always in integer type of value dipicting quotient part of the result after division using operator ‘//’. This operation is also known as Floor division. |
% | Num=5 Remainder=Num%2 Print(Remainder) |
1 | Divides Num by 2 – stores the remainder part produced after division operation performed using mathematical operator ‘%’ |
** | Num=3 Print(Num**3) |
1 | Operator “**” evaluates Num3 before printing |
Note – The kind of work that we do on computers is not 100% of mathematical nature, ut we can carry out 100%of mathematical operations inPython using Arithmetic Operators.
Arithmetic Operators are the most essential part of any programming language, without them any programming language would be incomplete. While doing these mathematical operations, python follows some Operator precedence rules.
Arithmetical Operators Precedence Rules
Highest to Lowest Priority | Operator | Associativity |
1 | () brackets | |
2 | + (unay positive), – (unary negation) | |
3 | ^ (raise to the power) | |
3 | * / // % | Left to Right |
4 | + – | Left to Right |
Observe the examples below, performing arithmetic operations
X=22+5*5 print(X)
Output:
47
This is evaluated as:
X=22+5*5
X=22+25
X=47
Since multiply operator (*) has higher priority than addition operator (+), 5*5 is evaluated first resulting in 25, and then 25 is added to 22 giving the output: 47
Learn more about arithmetical Operations used in programming.