Lingnan University · FACULTY OF COMPUTER SCIENCE

CDT501 Chap.3 Variables, Types and Expressions

- one subject, every graph, every model, every mark
5 Chapters5-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 3 of 8 · CDT501

Variables, Types and Expressions

Make state changes visible

This chapter treats assignment as evaluation followed by rebinding. It traces values and types line by line, separates numeric arithmetic from string concatenation and repetition, and makes conversion a documented claim about valid input.

The closing method calculates a moderated project score inside the outer course weighting so that syntactically valid arithmetic cannot hide a mistaken assessment model.

In this chapter

What this chapter covers

  • 01

    Variable names and bindings

  • 02

    Assignment and augmented assignment

  • 03

    Numeric and string types

  • 04

    Explicit input conversion

  • 05

    Weighted calculations with intermediate values

Worked example · free

Calculate a two-level weighted result

Q [8 marks]. Use assignment 76, teacher group 84, contribution 90 and final paper 81 with the published formulas. The mark allocation used here organises AskSia revision and is not an official University marking scheme.
The arrow represents evaluation followed by rebinding.The arrow represents evaluation followed by rebinding. Python reads the old value on the right, computes fifteen, and only then changes what the name x refers to.Assignment changes the current bindingbeforex = 10x = x + 5afterx = 15
The arrow represents evaluation followed by rebinding. Python reads the old value on the right, computes fifteen, and only then changes what the name x refers to.
  • 3Compute the moderated group result.
  • 3Apply the outer course weights.
  • 2Explain why contribution is not separate.
The moderated group result is 0.80 times 84 plus 0.20 times 90, which equals 85.2. The course result is 0.30 times 76 plus 0.30 times 85.2 plus 0.40 times 81, which equals 80.76. Contribution is nested inside the group component and must not be counted again.
Sia tip — Label the inner result before applying the outer weights.
Glossary

Key terms

Binding
The current association between a name and a value or object.
Expression
A combination of values and operations that Python evaluates to produce a result.
Integer
A whole-number value supporting arithmetic without a fractional part.
Float
A numerical value represented with a decimal or floating-point form.
Conversion
An explicit operation creating a value of a requested type from another representation.
FAQ

Variables, Types and Expressions FAQ

Why is x equals x plus one valid in Python?

Python reads it as assignment rather than a timeless mathematical equality. The right side uses the current value bound to x, adds one and produces a result. Only after that evaluation succeeds does the left side update the binding so x refers to the new value.

Why can multiplying text by a number produce text?

String multiplication means repetition, so a text value such as four repeated three times becomes three copies of the characters. Python follows the operand types rather than guessing that the text represents a quantity. Validate and convert the input before arithmetic when numerical meaning is intended.

How can I expose a wrong weighting formula?

Use unequal component values and make the teacher and contribution scores noticeably different. Equal values allow several incorrect formulas to return the same result. Print the moderated group value separately, verify that the outer weights total one, and compare the final number with a hand calculation.

Why should conversion happen at the input boundary?

Converting near the point of input gives the rest of the program a clear promise about type and validity. First preserve the raw value long enough to report a useful error, then check the permitted form and convert it once. If conversion is scattered across later expressions, different paths may interpret the same text differently, and an error can appear far from its cause.

Boundary conversion also makes tests sharper: one set checks rejected inputs, while the calculation tests can use already validated numbers. Do not convert merely because an operator fails; decide whether decimals, signs, blank text and surrounding spaces belong to the accepted domain, document that decision, and test each boundary explicitly.

Study strategy

Assessment move

Build a state table with one row per executed line and columns for value and type. Expand augmented assignment into its read, calculate and rebind meaning. For every input conversion, list one valid and two invalid strings. Recalculate the course-weight example with new unequal scores and verify the inner project formula before the outer weighted sum. Start each trace by copying only names that currently exist.

Evaluate the right-hand expression from that before-state, then update the binding on the left. Never revise an earlier row after a later reassignment; the whole point is to preserve sequence. Add separate columns for printed output and returned value so a visible number is not mistaken for stored state. Practise one expression with two integers, two strings and a mixed pair.

Predict addition, concatenation or an exception from the operand types before calculating. Repeat with multiplication to separate numerical scaling from string repetition. For division, use one even and one non-even case so ordinary and floor division cannot accidentally agree. At every input boundary, write the original text, validation rule and converted type.

Test empty text, decimal text and a word as well as a valid integer. Treat a failed conversion as information about the data rather than replacing it silently. For weighted results, select unequal scores and make the contribution score far from the teacher score. Compute the moderated group value as a named intermediate, check the outer weights sum to one, then compare the final result with an independent hand calculation.

Finally change only one component and predict the direction and size of the result change. This sensitivity check reveals whether the intended weight actually reaches the formula and whether a nested contribution has been counted twice.

Working through Variables, Types and Expressions in CDT501? Sia is AskSia’s AI Computer Science tutor — ask any CDT501 Variables, Types and Expressions question and get a clear, step-by-step explanation grounded in how CDT501 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 9 of your Lingnan University subjects - and 1,000+ Bibles across every Australian university.
Sia - your CDT501 tutor, unlimited, worked the way the exam marks it
The full 5-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works
CDT501 · Computer Systems and Programming Basics - independent study guide on the AskSia Library. More Lingnan University subjects · Microeconomics across all universities