COGNITION FOUNDRYLEARN. BUILD. UNDERSTAND.
PY-004 / Keeping track of values◷ 17 min read + practice

Variables and names

Hard-coding the same value everywhere makes programs difficult to maintain. Variables let us name values once and reuse them.

By the end, you can: Create and change variables, use names consistently and follow assignments from top to bottom.
Before this lesson: PY-003

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.

CF Python PlaygroundBrowser-based Python
Ready to load Python
Give values useful names

A single change updates the calculation.

Syntax colours highlight Python code. Tab inserts four spaces; press Escape then Tab to leave the editor. Ctrl+Enter runs the current code.
OUTPUT / ERRORS
Press Run Python to execute the editable code.
NOW MAKE IT YOURS

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.

  1. What does total = total + 1 mean 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 to total. For example, if total was 4, it becomes 5. The = sign is assignment, not an algebraic equality.

  2. Why might order_total be a better name than x?

    View answer

    order_total communicates what the value represents, making the code easier to read, review and maintain. x hides 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.