Skip to main content

Assignment operator

Note☆ Learning Objectives
  • Differentiate between an assignment operation and a mathematical equal \(=\)
  • Apply assignment operations (=, +=, -=, *=, /=, etc.) correctly.
  • Trace how the value of variables changes during program execution.

Assignment vesus Equality

Assignment operators are used to set the value of a variable, unlike in math where the symbol \(=\) means an equivalence/equality (e.g 5 + 3 = 8). However in Python, executing5 + 3 = 8 will cause a syntax error. This is because an assignment should be understood as a left arrow (←), i.e setting the result of the expression to that variable (variable ← value/ expression). The value or expression on the right hand side of the operator is evaluated first. Then the result of this expression is assigned to the variable on the left hand side. So running 5 + 3 in Python doesn’t make sense because an expression cannot be reassigned. The value or expression on the right hand side of the operator is evaluated first. Then, the result is assigned to the variable on the left hand side.

Python expects the left hand side of the equality to be a variable.

There exists much more assignment operators, but we will only focus on the following ones.

Table of assignment operators

Assignment Operator Effect on variable Description Example Result (if x = 10 initially)
= x ← value Assigns a new value to a variable. x = 5 x becomes 5
+= x ← x + value Adds a value to the variable and stores the result. x += 3 x becomes 13
-= x ← x - value Subtracts a value from the variable and stores the result. x -= 4 x becomes 6
*= x ← x * value Multiplies the variable by a value and stores the result. x *= 2 x becomes 20
/= x ← x / value Divides the variable by a value and stores the result. x /= 2 x becomes 5
%= x ← x % value Replaces the variable with the remainder after division. x %= 3 x becomes 1
**= x ← x ** value Raises the variable to a power and stores the result. x **= 2 x becomes 100
//= x ← x // value Performs floor (integer) division and stores the result. x //= 3 x becomes 3

Tracing a variable

Tracing a variable is to keep track of its value as its being assigned or reassigned during a program’s execution. You can use this tool: Codeboot to help visualize the impact of the assignment operators on variables, in the following examples.

Assignment =

Assigns a value to a variable (e.g.: name = "Julie" , result: assigns the value "Julie” to the variable name)

⏯️ Animate Execution

name = "Julie"
total  = 5 + 3
average = total / 2

⏯️ Animate Execution

5 + 3 = 8 # This will cause an error

Addition assignment +=

  • Performs an addition (numeric addition or string concatenation) on the two operands and assigns the result to the left operand.

⏯️ Animate Execution

value = 6
value += 2
print(value) # output: 8

⏯️ Animate Execution

name = "Emma"
name += "Watson"
print(name) # output: "EmmaWatson"

Subtraction assignment -=

  • Performs a subtraction on the two operands and assigns the result to the left operand.

⏯️ Animate Execution

value = 6
value -= 2
print(value) # output: 4

Multiplication assignment *=

  • Performs a multiplication on the two operands and assigns the result to the left operand.

⏯️ Animate Execution

value = 6
value *= 2
print(value) # output: 12

Division assignment /=

  • Performs a division on the two operands and assigns the result to the left operand.

⏯️ Animate Execution

value = 6
value /= 2
print(value) # output: 3

Tracing Challenge

🎯 Test Myself: Manually execute the operations below and write the expected result. Check your answers by copying the code into a Python editor or terminal.

⏯️ Animate Execution

day = 1
temperature  = 25.0
print(temperature)
day+=1
temperature -= 5.0
print(temperature)
day+=1
temperature = 23.5
print(temperature)
day+=1
temperature += 2.5
print(temperature)
day+=1
temperature -= 5.0
print(temperature)
day+=1
temperature /= 2
print(temperature)
day+=1
temperature *= 3

print("The weather in Canada is nuts...")
print(day, temperature)