Python follows the standard mathematical order of operations, often remembered as BODMAS (or PEMDAS):
| Priority | Operation | Symbol |
|---|---|---|
| 1 | Brackets | () |
| 2 | Orders (powers) | ** |
| 3 | Division / Multiplication | /, *, //, % |
| 4 | Addition / Subtraction | +, - |
Operations with the same priority are evaluated left to right.
result1 = 2 + 3 * 4
print(result1) # 14 (multiplication first, then addition)
result2 = (2 + 3) * 4
print(result2) # 20 (brackets first, then multiplication)
Use brackets to make your intentions clear and override the default order:
# Without brackets: might be confusing
result = 10 + 5 * 2 / 5 - 3
# With brackets: much clearer
result = 10 + ((5 * 2) / 5) - 3
round() FunctionWhen working with decimal numbers, you often get results with many digits after the decimal point. The round() function lets you control precision:
round(value, decimal_places)
Examples:
>>> round(3.14159, 2)
3.14
>>> round(2.71828, 3)
2.718
>>> round(10 / 3, 2)
3.33
>>> round(10 / 3)
3
If you omit the second argument, round() rounds to the nearest whole number.
distance = float(input("Enter distance: "))
time = float(input("Enter time: "))
speed = round(distance / time, 2)
print("Speed:", speed)