Python supports a full range of arithmetic operators:
| Operator | Operation | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 3 * 7 | 21 |
/ | Division | 15 / 4 | 3.75 |
// | Integer division | 15 // 4 | 3 |
% | Modulus (remainder) | 15 % 4 | 3 |
** | Exponentiation (power) | 2 ** 3 | 8 |
Regular division (/) always returns a float:
>>> 10 / 3
3.3333333333333335
>>> 10 / 2
5.0
Integer division (//) returns only the whole number part, discarding the remainder:
>>> 10 // 3
3
>>> 10 // 2
5
The modulus operator (%) returns the remainder after division:
>>> 10 % 3
1
>>> 15 % 4
3
>>> 20 % 5
0
This is very useful for checking divisibility (if the remainder is 0, the number divides evenly).
The ** operator raises a number to a power:
>>> 2 ** 3
8
>>> 5 ** 2
25
>>> 10 ** 0
1
length = float(input("Enter the length in cm: "))
width = float(input("Enter the width in cm: "))
print("The area is", length * width, "cm²")
total_seconds = int(input("Enter seconds: "))
hours = total_seconds // 3600
minutes = (total_seconds % 3600) // 60
seconds = total_seconds % 60
print(hours, "hours", minutes, "minutes", seconds, "seconds")