Variables, Types & Operators · Lesson 9

Variables and Assignment

A Python variable is a name bound to an object.

ConceptWorked examplePracticeKnowledge check
Textbook walkthrough

What Variables and Assignment actually means

A Python variable is a name bound to an object. Assignment with = does not copy a mathematical value into a fixed storage box; it makes a name refer to an object. Reassignment changes what the name refers to, while mutable objects may also change in place.

Variables and Assignment matters because every later calculation, condition and function operates on named objects with specific types. A wrong binding, conversion or operator can silently change the meaning of a program long before an obvious error appears.

Deeper walkthrough

Read Variables and Assignment as a mechanism, not a recipe

Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Evaluate the right-hand expression first. Stage 2: Create or reuse the resulting object. Stage 3: Bind the name on the left to that object. Final checkpoint: Multiple names can refer to the same mutable object, which is important when lists or dictionaries are modified.

Mechanism

Follow the transformation

Evaluate the right-hand expression first.

Create or reuse the resulting object.

Bind the name on the left to that object.

Evidence

Know what would convince you

  • Run the operation on a tiny literal input and write the expected type/value before executing it.
  • Inspect the relevant object state before and after the operation, especially when mutable objects are involved.
Useful distinctionAssignment x = 5: Bind x to the integer object 5.
Click a stage to inspect what happens, what changes, and what should be checked before moving on.
Stage 1

Evaluate the right-hand expression first

Evaluate the right-hand expression first. For Variables and Assignment, identify the exact state before this stage, the operation or rule applied here, and the observable state afterwards so the mechanism remains inspectable.

State focus: identify exactly what changed at this stage and what observable evidence confirms that change.
How it works

Trace the mechanism step by step

  1. Evaluate the right-hand expression first.
  2. Create or reuse the resulting object.
  3. Bind the name on the left to that object.
  4. A later assignment can rebind the same name to another object or type.
  5. Multiple names can refer to the same mutable object, which is important when lists or dictionaries are modified.
Worked demonstration

Make the concept concrete

Demonstration

Python example

# Step 1 — Compute the right-hand expression and store its result in `x` for the next step.
x = 6
# Step 2 — Compute the right-hand expression and store its result in `y` for the next step.
y = x
# Step 3 — Display the current value explicitly so the result/state can be inspected during execution.
print(x, y)
# Step 4 — Compute the right-hand expression and store its result in `x` for the next step.
x = 10
# Step 5 — Display the current value explicitly so the result/state can be inspected during execution.
print(x, y)       # y still refers to 6
# Step 6 — Compute the right-hand expression and store its result in `items` for the next step.
items = [1, 2]
# Step 7 — Compute the right-hand expression and store its result in `alias` for the next step.
alias = items
# Step 8 — Execute this statement and inspect how it changes the current value, object or program state.
alias.append(3)
# Step 9 — Display the current value explicitly so the result/state can be inspected during execution.
print(items)       # same mutable list object
Expected / illustrative result
6 6
10 6
[1, 2, 3]
Interpret the result.

For Variables and Assignment, connect the displayed result to the specific input and mechanism above; independently verify one value/state change rather than treating successful execution as proof.

Distinctions & related ideas

Know what this is — and what it is not

Assignment x = 5Bind x to the integer object 5.
Equality x == 5Compare values and produce True or False.
Mutation items.append(5)Change an existing mutable object rather than rebinding the name.
Use deliberately

When it is appropriate

Use Variables and Assignment when the program genuinely needs this language behaviour and you can state the input object, resulting value/state and expected failure behaviour.

Boundary conditions

When to stop or reconsider

Choose a clearer built-in, data structure or control-flow pattern when it expresses the intent more directly; stop if implicit conversion, mutation or hidden state makes the behaviour hard to reason about.

Common mistakes

Failure modes to recognise

  • Applying the operation to an incompatible type or assuming Python will silently coerce values the way you intended.
  • Confusing a returned value with an in-place mutation or other side effect.
  • Testing only the happy path and missing empty, boundary or invalid inputs.
Verification

How to check the result

  • Run the operation on a tiny literal input and write the expected type/value before executing it.
  • Inspect the relevant object state before and after the operation, especially when mutable objects are involved.
  • Try one boundary or invalid input and confirm that the returned value or exception matches the intended contract.
Hands-on practice

Demonstrate understanding

Try this:

Build a tiny, inspectable example of Variables and Assignment. First evaluate the right-hand expression first. Then create or reuse the resulting object. Write the expected result before running it, and explain one condition that would make the result misleading or invalid.

Use the smallest values that expose the rule. Write the expected value and type first, then compare Python’s actual state/output with that prediction.
Knowledge check

Check reasoning, not memorisation

Before trusting a result from Variables and Assignment, which check provides the strongest evidence that you understand and applied it correctly?

Quick reference

Keep the important distinctions visible

Step 1Evaluate the right-hand expression first.
Step 2Create or reuse the resulting object.
Step 3Bind the name on the left to that object.
Step 4A later assignment can rebind the same name to another object or type.
Lesson summary

What to remember

  • A Python variable is a name bound to an object. Assignment with = does not copy a mathematical value into a fixed storage box; it makes a name refer to an object. Reassignment changes what the name refers to, while mutable objects may also change in place.
  • Evaluate the right-hand expression first.
  • Applying the operation to an incompatible type or assuming Python will silently coerce values the way you intended.
  • Run the operation on a tiny literal input and write the expected type/value before executing it.