Operator Precedence (BODMAS)

Python follows the standard mathematical order of operations, often remembered as BODMAS (or PEMDAS):

PriorityOperationSymbol
1Brackets()
2Orders (powers)**
3Division / Multiplication/, *, //, %
4Addition / Subtraction+, -

Operations with the same priority are evaluated left to right.

Examples

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

The round() Function

When 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.

Combining Both

distance = float(input("Enter distance: "))
time = float(input("Enter time: "))
speed = round(distance / time, 2)
print("Speed:", speed)