Arithmetic
- Python course
- Beginner
- Lesson 24/116
- 4 min
Learn +, -, *, /, //, %, ** operators.
Arithmetic operators
Python has operators for arithmetic: + addition, - subtraction, * multiplication, / division.
Additionally: % modulo (remainder), ** power, // floor division. Floor division rounds down: 7 // 2 gives 3.
print(7 % 3) # 1 (remainder)
print(2 ** 3) # 8 (power)
print(7 // 2) # 3 (floor division)These operators work with int and float. Division always returns a float.
Power and floor division
** is the power operator: 2 ** 3 = 8. // is floor division: it rounds down.
print(5 ** 2) # 25
print(9 // 2) # 4 (4.5 down -> 4)
print(-9 // 2) # -5 (-4.5 down -> -5)Floor division needs care with negatives: -9 // 2 gives -5 because -4.5 is rounded down.
Practice Print arithmetic operations
Assign a = 10 and b = 3. Print the remainder with print(a % b) and floor division with print(a // b).
Starter code
# Arithmetic operationsSample solution
a = 10
b = 3
print(a % b)
print(a // b)Expected output
1 3
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is 7 % 3?
- 2
- 1 Correct answer
- 3
- 0
Explanation: The remainder of 7 divided by 3 is 1.
2 Complete the power operation
result = 2 ____ 3
print(result)
result = 2 ** 3
print(result)
Explanation: ** is the power operator.
3 What is 2 ** 3?
- 6
- 9
- 8 Correct answer
- 5
Explanation: ** is the power operator.