Arithmetic operators
Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication, etc.
Operator | Meaning | Example |
+ | Add two operands or unary plus | x + y+2 |
– | Subtract right operand from the left or unary minus | x – y-2 |
* | Multiply two operands | x * y |
/ | Divide left operand by the right one (always results into float) | x / y |
% | Modulus – the remainder of the division of left operand by the right | x % y (remainder of x/y) >>> 10%6 4 >>> 10%7 3 >>> 10%3 1 |
// | Floor division – division that results into the whole number adjusted to the left in the number line | x // y >>> 5//2 2 >>> 5/2 2.5 |
** | Exponent – left operand raised to the power of right | x**y (x to the power y) print(2**3) 8 Print(3**2) 9 |
Example:1
a=10 b=20 c=a+b print("Sum ",c) c=a*b print("Prod ",c) c=a-b print("Diff ",c) c=(a+b)/2 print("Avg ",c)
Output:
Sum 30 Prod 200 Diff -10 Avg 15.0 >>>
Example:2
print(10%3) print(20%2) print(20%3) print(10%6) print(20%6)
Output:
1 0 2 4 2 >>>
Example:3
#1 print(10/3) print(10//3) #2 print(20/8) print(20//8) #3 print(20/3) print(20//3) #4 print(45/4) print(45//4)
Output:
3.3333333333333335 3 2.5 2 6.666666666666667 6 11.25 11 >>>
Example:4
a=10**2 print(a) a=5**2 print(a) print(10**3) print(5**3)
Output:
100 25 1000 125 >>>