Python Tutorial – Arithmetic


Python’s Arithmetic Operators ✈️

+Addition
Subtraction
*Multiplication
/Division
//integer division
%Modulo
**Exponentiation
( )Parentheses can change the order of operation precedence

Order of Precedence of Arithmetic Operators

1. ()
2. **
3. *, /, %, //
4. +, -
The priority order is 1 > 2 > 3 > 4 and higher operators will be calculated first. If they are in the same priority order, the order of operators is the order in which they appear in the expression. If you want to change the order of operations, you can use ().

Demonstration of Various Operators

Example

"""
arithmetic operators
"""
print('### + ### ')
x = 1 + 1
print(x)
x = 1 + 1.1
print(x)

print('### - ### ')
x = 1 - 1
print(x)
x = 1 - 2.0
print(x)

print('### * ### ')
x = 1 * 1
print(x)
x = 1 * 2.0
print(x)

print('### / ### ')
x = 1 / 1
print(x)
x = 1 / 2.0
print(x)

print('### //, **, % ### ')
x = 1 // 1
print(x)
x = 2 ** 2
print(x)
x = 1 % 2
print(x)

print('### () ### ')
x = 1 + 2 * 3
print(x)
x = (1 + 2) * 3
print(x)
x = 2 * 3 ** 4
print(x)
x = (2 * 3) ** 4
print(x)

Result

### + ### 
2
2.1
### - ### 
0
-1.0
### * ### 
1
2.0
### / ### 
1.0
0.5
### //, **, % ### 
1
4
1
### () ### 
7
9
162
1296