3.2.2 - Variables, constants and sequence (Python)
Programs store values under names and execute statements in a deliberate order. You will use variables, the Python convention for named constants and meaningful identifiers to write and interpret a short calculation.
Variables and named constants
A variable gives a value a name so that a later statement can use or replace that value.
Variable
A named storage location whose value can change while a program runs.
In Python, the first assignment creates the variable; a separate declaration specifying its type is not required.
ticket_count = 3
ticket_count = 4
TICKET_PRICE = 6
After the second statement, ticket_count holds 4. The earlier value 3 has been replaced. A named constant represents a value intended to stay the same, such as a price or maximum permitted score. Python convention uses capital identifiers such as TICKET_PRICE. Python does not enforce immutability just because a name is capitalised: the programmer must avoid assigning a new value to it. Other languages may have an enforced constant declaration.
A named price is easier to understand than an unexplained 6, and changing one definition avoids inconsistent prices scattered through a program. A variable is suitable for an input quantity that differs from one order to another.
Assignment and sequence
Each assignment evaluates the right-hand expression before storing the result under the name on the left.
Python uses = for assignment. It is not a mathematical claim that two expressions are permanently equal. The statement ticket_count = ticket_count + 1 reads the old count, adds one and replaces it with the new count.
TICKET_PRICE = 6
ticket_count = 3
total = ticket_count * TICKET_PRICE
ticket_count = ticket_count + 1
print(total, ticket_count)
Sequence means statements execute in order. The calculation stores 18 in total before the count changes to 4, so the output is 18 4. total does not automatically recalculate when another variable changes. Moving its assignment below the update would store 24 instead.
Design the order from the dependencies: establish the price and count, calculate the total, then display it. The sandbox lets you test and refine that order. Predict its output, run it, then move the total calculation below the count update and compare the result.
When is the total calculated?
{"code":"TICKET_PRICE = 6\nticket_count = 3\ntotal = ticket_count * TICKET_PRICE\nticket_count = ticket_count + 1\nprint(total, ticket_count)","instructions":"Predict and run. Move total = ticket_count * TICKET_PRICE below the count update, then run and explain the changed total."}
Meaningful identifiers
A meaningful identifier communicates what a value or operation represents.
Identifiers name variables, constants and subroutines. carton_count describes a changing count, CARTON_PRICE signals an intended constant, and a subroutine name such as calculate_total describes its job. Compare x = y * z with the named multiplication above: both can perform the same arithmetic, but meaningful names let another programmer check the calculation against the problem and find errors more easily.
Python identifiers can contain letters, digits and underscores, but cannot start with a digit or be a reserved keyword such as if. They are case-sensitive: total_cost and Total_Cost are different names. Use a consistent spelling and names appropriate to the task. A short loop counter can be reasonable when its role is clear; renaming every value to a long sentence makes code harder to read.
Use variables for changing data and named constants for fixed design values. Compute a result after the values it depends on are ready.