Python Increment & Decrement
In this page:
No ++ or -- in Python
Python deliberately has no ++ or -- operator — writing x++ is a syntax error, not a working statement with unexpected behavior. The idiomatic way to increase or decrease a variable is x += 1 or x -= 1 instead.
Example: No ++ or -- in Python
x = 5
# x++ # SyntaxError: invalid syntax
x += 1
print(x)
Incremental Addition
+= lets you add any amount, not just one, in a single step: score += 10 immediately updates score to reflect the new total without a separate addition and reassignment line.
Example: Incremental Addition
score = 0
score += 10
print(score)
Decremental Subtraction
-= mirrors += for subtraction, commonly used to count down remaining attempts, lives, or retries by decreasing a variable by a fixed or computed amount each time.
Example: Decremental Subtraction
lives = 3
lives -= 1
print(lives)
Incrementing List Items
To update a value stored inside a list, first access it by index and then apply the compound operator directly to that indexed position, like scores[0] += 5, which reads and rewrites that single list slot in place.
Example: Incrementing List Items
scores = [10, 20, 30]
scores[0] += 5
print(scores)
Other Combined Assignments
The same compound pattern extends to multiplication and division — *= scales a variable up and /= scales it down — giving Python a consistent shorthand family for "apply this operation, then store the result back."
Example: Other Combined Assignments
value = 4
value *= 2
print(value)
value /= 4
print(value)
Chapter Quiz — Complete all 16 topics to unlock
0/16 topics done
Complete these topics first:
- Python print()
- Python input()
- Python Format Strings
- Python f-strings
- Python String Formatting
- Python Arithmetic Operators
- Python Relational Operators
- Python Logical Operators
- Python Bitwise Operators
- Python Assignment Operators
- Python Increment & Decrement
- Python Ternary Operator
- Python Operator Precedence
- Python Identity Operators
- Python Membership Operators
- Python Operators