Variables and names
Hard-coding the same value everywhere makes programs difficult to maintain. Variables let us name values once and reuse them.
Assignment gives a name to a value
In total = 10, the = sign assigns the value 10 to the name total. This is not an algebra equation: Python performs the assignment as an instruction.
When you later print(total), Python looks up the value associated with that name. Names are case-sensitive: total and Total are different names.
Reassignment happens in sequence
You can reassign a name as the program runs. count = count + 1 means “read the current count, add one, and store the new value”. It does not mean count equals count plus one for all time.
When following a program, read assignments in order. Most beginner errors become easier to diagnose once you track the value after each statement.
Names are part of your documentation
Prefer descriptive snake_case names such as item_count over x when the purpose is not obvious. Avoid spaces, starting with a digit, or using a Python keyword as a name.
A well-named variable explains what a calculation represents. Later, functions will help group those calculations into reusable operations.
Try it yourself.
Choose a starting example or write something completely different. You control the code; the Run button executes what is in the editor, not a hidden preset.
A single change updates the calculation.
Press Run Python to execute the editable code.
Change quantity from 3 to 5 and explain the new total.
Need a hint?
Only the assignment line needs changing.
On first Run, a pinned Python runtime downloads from jsDelivr. Code executes locally in your browser's Web Worker, not on our server. It cannot inspect your normal PC files or Windows hardware. Avoid entering secrets. Heavy/infinite loops are stopped by a time limit.
Check your understanding.
Try answering each question before opening its reference answer. Explain the idea aloud or change an example to test it for yourself.
What does
total = total + 1mean in a running program?View answer
The right side is evaluated first using the *current* value of
total; Python adds one, then assigns the result back tototal. For example, iftotalwas 4, it becomes 5. The=sign is assignment, not an algebraic equality.Why might order_total be a better name than x?
View answer
order_totalcommunicates what the value represents, making the code easier to read, review and maintain.xhides that meaning and gets harder to follow as a program grows. Short names are fine where their meaning is obvious.
Where this leads.
Names can store text as well as numbers. Next we will manipulate useful strings.