OperatorsBeginner7 min16 / 63

Assignment Operators

Learn how assignment operators let you update variables quickly and cleanly — from the basic = to handy shortcuts like += and *=.

Every program needs to keep track of changing values. A score goes up. A countdown ticks down. A message gets a new word added. Assignment operators are the tools Python gives you to update variables without rewriting them from scratch.

You already know the basic one: =. But Python has a whole family of shortcuts that make your code shorter, cleaner, and easier to read.

See it in action

Visual walkthrough1 / 5
1

Update Variables Without Rewriting Them

Every program tracks changing values — scores, totals, counters. Assignment operators are Python's tools for updating a variable without starting from scratch.

You already know = — now meet its whole family of shortcuts.

#The Basic Assignment Operator

The = operator stores a value in a variable. Think of a variable as a labeled box, and = as the action of putting something inside it.

= puts a value into a variable
score = 0
name = "Alex"
print(score)
print(name)
Note

= is not math equality

In Python, = means store this value. It does not mean "is equal to". To check if two things are equal, Python uses == (two equals signs). These are completely different things!

#Updating a Variable the Long Way

Suppose you have a score of 10 and you want to add 5 to it. You could write it like this:

Updating a variable by referring to itself
score = 10
score = score + 5
print(score)

This works perfectly fine. But notice that score appears twice on that second line. Python reads the right side first (score + 5 = 15), then stores the result back into score. It is correct — just a bit repetitive.

#Meet the Augmented Assignment Operators

Think of it like

Like a tally counter

Imagine a hand-held tally counter. Every time you press the button, it adds 1 to whatever number is already showing. You do not type the old number in — you just press +1. Augmented assignment operators work exactly like that button.

Python lets you combine an operation and an assignment into one step using augmented assignment operators. The most common ones are:

  • += — add and assign
  • -= — subtract and assign
  • *= — multiply and assign
  • /= — divide and assign
  • //= — floor divide and assign
  • %= — modulo and assign
  • **= — exponentiate and assign
+= adds to the current value
score = 10
score += 5   # same as: score = score + 5
print(score)

#All the Augmented Operators in Action

Every augmented operator updates x in place
x = 20

x -= 4    # subtract
print(x)  # 16

x *= 3    # multiply
print(x)  # 48

x /= 6    # divide (result is a float)
print(x)  # 8.0

x //= 3   # floor divide (no decimal)
print(x)  # 2.0

x **= 4   # raise to a power
print(x)  # 16.0

x %= 5    # remainder after dividing by 5
print(x)  # 1.0
Tip

Use += in loops and counters

Augmented assignment really shines inside loops. Whenever you need a counter or a running total, += is your best friend. It keeps the code clean and easy to scan.

#Using += in a Loop

Accumulating a total with +=
total = 0
for number in [10, 25, 5, 60]:
    total += number

print(total)

Each time the loop runs, total += number adds the current number to whatever total already holds. By the end, total has accumulated all four values.

#It Works With Strings and Lists Too

+= is not just for numbers. You can use it to append text to a string or extend a list — the same idea applies.

+= builds up a string piece by piece
message = "Hello"
message += ", world"
message += "!"
print(message)
+= adds elements to a list
items = ["apple", "banana"]
items += ["cherry"]
print(items)
Common mistake

You must assign a value first

You cannot use an augmented operator on a variable that does not exist yet. This will crash:

``python # WRONG — counter was never defined counter += 1 ``

Always initialize the variable first:

``python counter = 0 # start here counter += 1 # now this works ``

Quick check

What is the value of `x` after this code runs? ```python x = 8 x *= 3 x -= 4 ```

Key takeaways

  • `=` stores a value in a variable — it is not a math equality check.
  • `x += 5` is shorthand for `x = x + 5` — it reads and updates the variable in one step.
  • Python has seven augmented operators: `+=`, `-=`, `*=`, `/=`, `//=`, `%=`, and `**=`.
  • `+=` works with strings and lists, not just numbers.
  • Always initialize a variable before using an augmented operator on it.
Practice challenges
Test yourself · earn XP
0/4
Predict the output#1

What does this code print?

predict-output
score = 10
score += 5
score *= 2
print(score)
Fix the bug#2

This code is supposed to count up to 3 but crashes. What is wrong?

fix-bug
for i in range(1, 4):
    counter += 1
print(counter)
Fill in the blank#3

Complete the code so it builds the message "Hello, world!" using += twice.

message = "Hello"
message  ", world"
message  "!"
print(message)
Reorder the lines#4

Put these lines in the right order to add up a list of prices and print the total.

1
total = 0
2
    total += price
3
for price in [5, 12, 3, 8]:
4
print(total)
Your turn
Practice exercise

Write a program that starts with a coins variable set to 100. Then: 1. Add 50 coins (a bonus reward). 2. Subtract 30 coins (a purchase). 3. Double the remaining coins (a multiplier power-up). 4. Print the final number of coins.

Try it live — edit the code and hit Run to execute real Python:

solution.py · editable