4.1.1.2, 4.1.1.3, 4.1.1.6 - Variables, constants and arithmetic (Python)

4.1.1.2, 4.1.1.3, 4.1.1.6 - Variables, constants and arithmetic (Python)

Follow assignments in sequence, use meaningful names and constants, and choose arithmetic operations that match the required result.

Assignment and sequence

An assignment evaluates the expression on its right and binds the resulting value to the name on its left. score = score + 5 reads the old score before replacing its binding; it does not assert an algebraic equality.

Python
score = 10
bonus = 5
score = score + bonus
bonus = 2
print(score, bonus)

The output is 15 2. Changing bonus does not recompute an earlier expression. Statements run in sequence, and using a name before assigning it raises NameError. Python does not require a separate declaration before ordinary assignment. An optional annotation such as score: int = 10 describes the intended type but does not enforce a fixed runtime type.

Meaningful identifiers explain a value's role: distance_metres is more useful than x. They reduce misinterpretation and help someone inspect, debug or maintain a program. Names are case-sensitive, cannot contain spaces or start with a digit, and must not be reserved words such as if.

input() reads one line as a string. To calculate with a guaranteed whole-number input, use count = int(input()); for a fractional input use measurement = float(input()). print() displays a value. Conversion and error handling are developed in later lessons; these basic input/output operations let you supply changing values to an arithmetic program.

Sequence, selection and iteration are the three basic ways of combining imperative statements. Selection chooses a branch; iteration repeats statements. A subroutine groups statements behind a name so they can be called where needed. The next lessons develop branches and loops; the subroutines lesson develops parameters and results. Here the assessed demand is state changes in sequence.

Named constants

A variable is intended to take different values during execution, while a constant is intended to retain its value. Python normally represents a named constant by an uppercase name and a convention to leave it unchanged:

Python
METRES_PER_KILOMETRE = 1000
distance_km = 2.5
distance_metres = distance_km * METRES_PER_KILOMETRE
print(distance_metres)

Python still permits reassignment of METRES_PER_KILOMETRE; uppercase spelling is not runtime protection. Nevertheless, the name makes the conversion's meaning clear. If a configurable rate changes, updating one definition is safer than searching through several unrelated occurrences of the same literal. One shared definition also prevents inconsistent rates in different calculations. A constant need not be a number: DEFAULT_CATEGORY = "standard" is also useful.

Use a named constant for one stable meaning and a variable for changing state. Consistent use of the name gives the maintenance benefit; declaring it and continuing to use duplicated literals does not.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account