SythraOpen app

Prerequisites to Iteration in Python (Variable Updates)

Before writing loops, you need to understand that Python variables can be reassigned freely, and an update like x = x + 1 uses a variable's current value to compute its next one — shorthand as x += 1.

Sythra

6 min read

XLinkedIn
Prerequisites to Iteration in Python (Variable Updates) — cover illustration

Every program you've written so far runs top to bottom — one line, then the next, until it's done, each line executing exactly once. That works fine for small tasks, but imagine printing the numbers from 1 to 1000. Would you write a thousand separate print statements? Nobody has time for that. What you need is iteration — the ability to repeat, to tell Python "keep doing this until I say stop."

Before reaching the while loop and the for loop, there's one prerequisite worth locking in: how variable reassignment and updates work in Python, since every loop depends on them.

What you will learn

  • Why variables in Python can be reassigned as many times as you like
  • Why assignment in Python is not the same as equality in math
  • How to update a variable using its own current value
  • The rule that a variable must exist before you update it
  • Shorthand update operators: +=, -=, *=, /=

Multiple assignment: variables can change their minds

Variables in Python can be reassigned. You are not locked in once you give a variable a value — you can change it as many times as you like:

bruce = 5
print(bruce, end=" ")   # prints 5
bruce = 7
print(bruce)            # prints 7

# Output: 5 7

Both numbers print on the same line because end=" " tells Python to put a space instead of a new line after the first print. Each time you assign a new value, the old one is gone — the variable name stays the same, but what it points to has changed. Picture it like a sticky label: you peel it off one box and stick it onto a different box.

There's another difference too. In math, if a = b is true today, it stays true forever. Python makes no such promise:

a = 5
b = a     # both are 5 right now
a = 3     # a is now 3, but b is still 5

Changing a afterward does not change b. They shared the same value for a brief moment — not a lifelong connection. Once the assignment happens, the two variables are completely independent of each other.

Updating variables: using the old value to get a new one

The most common use of reassignment is called an update — where the new value of a variable is calculated using its own current value:

x = x + 1

Read this out loud as: "take x, add 1 to it, and store the result back into x." This particular kind of update is called an increment. The opposite — subtracting 1 — is called a decrement.

Always initialize your variable first, and only then update it:

x = 0       # initialize
x = x + 1   # now update — x becomes 1
x = x + 1   # x becomes 2

Python also gives you shorthand versions of these common updates, so you don't have to type the variable name twice:

x += 1    # same as x = x + 1
x -= 1    # same as x = x - 1
x *= 2    # same as x = x * 2
x /= 2    # same as x = x / 2

These shorthand operators are the backbone of every loop you're about to write — a counter that increments with += 1 on each pass is the single most common pattern in while loops and for loops alike.

Common mistakes

  • Trying to update a variable before it has ever been assigned a value
  • Writing 7 = a instead of a = 7, expecting math-style equality to work both ways
  • Assuming b = a keeps b permanently linked to a — it only copies the value at that moment
  • Forgetting that x += 1 and x = x + 1 are exactly equivalent, and mixing styles inconsistently

Common questions

Can you reassign a variable in Python?

Yes. Python variables can be reassigned as many times as you like. Each new assignment simply replaces the old value — the variable name stays the same, but what it points to changes.

What does x = x + 1 mean in Python?

It means: take the current value of x, add 1 to it, and store the result back into x. This is called an increment, and it requires x to already exist — otherwise Python raises a NameError.

What is the difference between x += 1 and x = x + 1?

They do exactly the same thing. x += 1 is shorthand for x = x + 1, saving you from typing the variable name twice.

If b = a, does changing a later change b too?

No. b = a copies a's value into b at that moment. After that, a and b are independent — changing a afterward does not affect b.

Why does x = x + 1 raise a NameError sometimes?

Python evaluates the right-hand side of an assignment first. If x has never been assigned a value before, there's nothing to add 1 to, so Python raises NameError: name 'x' is not defined. Always initialize a variable before updating it.

Explore

Related topics

Keep going — these sit next to this concept in a real learning path.

Browse all python explainers →