Guidelines

What Do +=, -= Mean?

Operators like +=, -=, which combine an operator with an = sign, are called Compound Assignment Operators.

For instance, x += y means the same as x = x + y, where the value of x is updated by adding y to x.

Example of Compound Assignment Operators
x = 10 y = 5 x += y # x = x + y print(x) # 15

Types of Compound Assignment Operators

In Python, compound assignment operators include += (addition), -= (subtraction), *= (multiplication), /= (division), %= (modulus), and others.

Example of Compound Assignment Operators
number = 10 print("number:", number) # 10 number += 5 # number = number + 5 print("number += 5:", number) # 15 number -= 3 # number = number - 3 print("number -= 3:", number) # 12 number *= 2 # number = number * 2 print("number *= 2:", number) # 24 number /= 4 # number = number / 4 print("number /= 4:", number) # 6.0 number %= 2 # number = number % 2 print("number %= 2:", number) # 0.0

Compound assignment operators are often used in loops, such as when adding numbers from 1 to 5.

Note: For more details about loops, refer to the lesson How to Perform Repetitive Tasks in Python Based on Conditions.

Example of Compound Assignment Operators
numbers = [1, 2, 3, 4, 5] total = 0 for num in numbers: total += num print("total:", total) # 15
Mission
0 / 1

What does x -= y mean?

`x -= y` is equivalent to `x = `.
x + y
x - y
x * y
x / y

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result