Variables and arithmetic order
Name values, work with basic types, and understand Python operator precedence.
Names and values
Assignment binds a name to a value. Python values have types such as int, float, str, and bool:
items = 3
unit_price = 2.5
label = "notebook"
in_stock = True
total = items * unit_price
print(f"{items} {label}s cost {total}")
Use descriptive names and remember that = assigns, while == compares two values.
BODMAS, PEMDAS, and Python
BODMAS and PEMDAS are memory aids for arithmetic order; they are not Python syntax. Python follows its own documented precedence. For the arithmetic covered here:
- Parentheses group an expression.
- Exponentiation (
**) binds more tightly than multiplication and addition. - Multiplication (
*), division (/), floor division (//), and remainder (%) share a precedence level and evaluate left to right. - Addition (
+) and subtraction (-) share a lower precedence level and evaluate left to right.
For example:
print(2 + 3 * 4) # 14
print((2 + 3) * 4) # 20
print(20 / 5 * 2) # 8.0: same precedence, left to right
Use parentheses when they make your intent clearer, even if precedence already determines the result. See the operator precedence reference for the complete table.
Exercise: Predict the result of 18 - 2 * 3 ** 2, then verify it in Python. Add parentheses to make a different order explicit.
Sources: Python Tutorial: An Informal Introduction, Python expression reference: Operator precedence, LearnPython.org: Variables and Types