COMP90059 Chap.2 Values, Variables and Expressions
Values, Variables and Expressions
Python operators obtain their meaning from operand types. Addition combines numbers but concatenates strings; multiplication combines numbers but can repeat a string when paired with an integer. Explicit conversion creates a new representation and must match the incoming text. Operator precedence and associativity determine grouping before arithmetic begins, with exponentiation binding tightly and grouping from the right.
Floor division differs from truncation for negative values, while remainder preserves the quotient-remainder identity. Floating-point values add another layer: many decimal fractions are stored as nearby binary approximations, so exact equality may ask the wrong question. A complete expression trace therefore records grouping, intermediate values, types and the comparison rule appropriate to the data.
What this chapter covers
- 01
Strings and numeric values support different operations
- 02
Variables are names bound to values rather than declared storage boxes
- 03
Int, float, str and bool perform explicit conversions
- 04
Precedence chooses which operator binds before another
- 05
Associativity resolves operators at the same precedence level
- 06
Floor division rounds toward negative infinity
- 07
Remainder works with quotient to reconstruct the dividend
- 08
Floating-point comparison often requires a meaningful tolerance
- 09
Augmented assignment still evaluates an operation before rebinding
- 10
Parentheses communicate intended grouping as well as enforce it
- 11
Conversion may lose information when moving to a narrower type
- 12
Formatted output should be separated from the value used in later calculations
Worked example · free
Evaluate a mixed operator expression
- 1Exponentiation binds before unary minus, giving -(2 ** 2) = -4.
- 1Floor division and multiplication share precedence and evaluate left to right.
- 1Compute 18 // 5 = 3, then 3 * 3 = 9.
- 1Add -4 and 9 to obtain 5.
- 1State that parentheses around -2 would change the first term and final result.
Key terms
- Operand
- A value on which an operator acts, such as the two values surrounding a binary plus sign.
- Precedence
- The rule deciding which operator binds first when parentheses do not already determine grouping.
- Associativity
- The direction used to group operators of equal precedence, such as right grouping for exponentiation.
- Floor division
- Division that returns the floor of the quotient, which rounds downward for negative results.
- Floating point
- A finite binary representation that approximates many real-number values and introduces predictable rounding effects.
- Tolerance
- An allowed numerical difference used to decide whether approximate results are sufficiently close for a task.
- Type conversion
- Creating a value of another type, with possible information loss such as discarding a fractional part when converting a float to an integer.
- Augmented assignment
- A compact update such as plus-equals that combines the current binding with a right-hand value and then rebinds the target name.
- Display precision
- The number of digits selected for a textual representation, which does not by itself alter the numeric value retained for later operations.
Values, Variables and Expressions FAQ
Why does adding two quoted numbers not add them?
Quoted values are strings, so plus concatenates their characters. Convert the strings to an appropriate numeric type before applying numeric addition, and handle any conversion failure at the input boundary.
Why is negative floor division surprising?
Floor division rounds toward negative infinity, not toward zero. For example, the floor of negative three and a half is negative four, so sign and rounding direction must both be traced.
Should floating-point values never be compared?
They can be compared, but the comparison must match the task. Exact equality suits exact representations; approximate calculations often need isclose with a justified tolerance that reflects the problem's acceptable error.
Does formatting change the underlying number?
No. A format specification controls the displayed string. The stored integer or floating-point value remains available at its original precision for subsequent arithmetic and comparisons.
How can I check a complicated expression reliably?
Rewrite it with explicit parentheses, identify the type of each operand and evaluate one operation at a time. Compare the trace with Python only after recording the prediction, because the disagreement pinpoints the misunderstood precedence, conversion or rounding rule.
How should I choose between integer and floating-point arithmetic?
Start from what the quantity represents and which operations it needs. Counts and exact discrete units usually suit integers; measured values and non-integral division usually require floating point. Keep conversion points visible, avoid using formatted strings as calculation inputs and test negative, zero and boundary values where rounding or division behaviour can change.
Exam move
Create an operator trace sheet with one example for conversion, precedence, quotient and remainder, and floating-point comparison. For every expression, write the fully parenthesised form, then the value and type after each operation. Verify quotient-remainder examples by reconstructing the original dividend. Test a negative floor-division case so that rounding direction is explicit.
Compare one exact integer calculation with a mathematically equivalent route through logarithms or decimal fractions, then explain why their stored representations differ. Finally, design a conversion boundary for user input: accepted text, failed text and the message or action that follows. The aim is to replace visual familiarity with a repeatable inside-out evaluation method.
Working through Values, Variables and Expressions in COMP90059? Sia is AskSia’s AI Computer Science tutor — ask any COMP90059 Values, Variables and Expressions question and get a clear, step-by-step explanation grounded in how COMP90059 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.