Python has no increment or decrement operators. There is no ++ and no --. You use augmented assignment instead:
count += 1 # instead of count++
count -= 1 # instead of count--
The dangerous part is that ++count does not raise an error. It’s valid Python that does absolutely nothing, so the bug is silent.
Every result on this page comes from running the code on Python 3.12.5.
Why does python ++ give a SyntaxError?
Writing count++ stops the program before it starts:
count = 5
count++ # this is not Python
Output:
File "C:\pyguides\python_plusplus_error.py", line 3
count++ # this is not Python
^^^^^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax

SyntaxError: invalid syntax, with the caret under the second +.Python’s designers left ++ out deliberately. It exists in C mainly to distinguish pre-increment from post-increment, and that distinction causes more bugs than it solves.
+= does the same job with no ambiguity about when the value changes.
Why ++x does not raise an error in Python
This is the part that costs people an afternoon. ++x is legal, and it is not an increment:
x = 5
# ++x does NOT raise. It is unary plus applied twice, so it does nothing at all.
print("++x =", ++x)
print("--x =", --x) # unary minus twice, also a no-op
print("-x =", -x)
print("\nx is still:", x, "<- nothing was incremented")
import dis
print("\nwhat ++x actually compiles to:")
dis.dis(compile("++x", "<example>", "eval"))
Output:
++x = 5
--x = 5
-x = -5
x is still: 5 <- nothing was incremented
what ++x actually compiles to:
0 0 RESUME 0
1 2 LOAD_NAME 0 (x)
4 CALL_INTRINSIC_1 5 (INTRINSIC_UNARY_POSITIVE)
6 CALL_INTRINSIC_1 5 (INTRINSIC_UNARY_POSITIVE)
8 RETURN_VALUE

INTRINSIC_UNARY_POSITIVE operations and no addition.Python reads ++x as +(+x), the unary plus sign applied twice. The value of +5 is 5, and the value of +(+5) is still 5.
--x is the same story with minus signs. Two negatives cancel, so it also returns the original value.
Nothing is assigned back to x either, so the variable never changes. No error, no warning, no effect.
The infinite loop that ++i causes in Python
A C-style while loop translated literally into Python never ends:
# what a C-style loop does in Python: nothing, forever
i = 0
iterations = 0
while i < 3:
++i # looks like an increment, changes nothing
iterations += 1
if iterations > 5:
print("stopped manually after", iterations, "passes -- i is still", i)
break
print()
# the working version
i = 0
while i < 3:
i += 1
print("with i += 1, the loop ended with i =", i)
Output:
stopped manually after 6 passes -- i is still 0
with i += 1, the loop ended with i = 3

i is still 0. The loop condition can never become false.This is the single best reason to know about unary plus. The symptom is a program that hangs, with no traceback pointing at the cause.
If a loop of yours never finishes, check for a ++ before you check anything else.
How to increment a variable by 1 in Python
+= is the whole answer, and it takes any step rather than just one:
count = 5
count += 1 # the Python way to increment
print("after += 1 :", count)
count -= 1 # and to decrement
print("after -= 1 :", count)
count += 10 # any step you like, which ++ could never do
print("after += 10:", count)
# += is just shorthand
count = count + 1
print("long form :", count)
Output:
after += 1 : 6
after -= 1 : 5
after += 10: 15
long form : 16
| C or Java | Python |
|---|---|
i++ | i += 1 |
i-- | i -= 1 |
i += 5 | i += 5 |
i *= 2 | i *= 2 |
There is no post-increment form, so you cannot use the old value and update it in one expression. Write the two steps out, which is clearer anyway.
Why += creates a new integer object in Python
Python integers are immutable, so += doesn’t modify the number. It builds a new one and points the name at it:
x = 5
print("id before:", id(x))
x += 1
print("id after :", id(x), "<- a DIFFERENT object")
print()
# lists are mutable, so += changes the same object
items = [1, 2]
print("list id before:", id(items))
items += [3]
print("list id after :", id(items), "<- the SAME object")
print("items:", items)
Output:
id before: 140718619953720
id after : 140718619953752 <- a DIFFERENT object
list id before: 2115398013312
list id after : 2115398013312 <- the SAME object
items: [1, 2, 3]

id for the integer, the same id for the list.Lists behave differently because they’re mutable. items += [3] extends the existing list in place, which is why the id stays the same.
It rarely matters in practice, but it explains why you cannot write a function that increments an integer passed into it.
Incrementing in a for loop
Most manual counters in Python are unnecessary. The language gives you the index or the step directly:
# you almost never increment a counter by hand in Python
fruits = ["apple", "banana", "cherry"]
print("enumerate gives you the index for free:")
for index, fruit in enumerate(fruits):
print(f" {index}: {fruit}")
print("\nrange with a step counts down:")
for n in range(5, 0, -1):
print(" ", n, end="")
print()
print("\nrange with a step of 2:")
for n in range(0, 10, 2):
print(" ", n, end="")
print()
Output:
enumerate gives you the index for free:
0: apple
1: banana
2: cherry
range with a step counts down:
5 4 3 2 1
range with a step of 2:
0 2 4 6 8
enumerate(items)— the index and the value together, no counter needed.range(0, 10, 2)— counts in twos.range(5, 0, -1)— counts down, which replaces a decrement loop.whilewithi += 1— only when the step isn’t regular.
If you find yourself writing i = 0 above a loop and i += 1 inside it, enumerate is almost always what you wanted. The same thinking applies to list comprehensions.
Counting in Python without a counter variable
For the two jobs people usually write increments for, the standard library already has a tool:
from collections import Counter
from itertools import count
# counting occurrences without a manual counter
words = ["red", "blue", "red", "green", "red"]
print("Counter:", Counter(words))
# an endless counter, when you really do need a running number
ticket = count(start=100, step=5)
print("\nitertools.count:", [next(ticket) for _ in range(4)])
# incrementing a value inside a dictionary
scores = {}
for word in words:
scores[word] = scores.get(word, 0) + 1
print("\nmanual dict increment:", scores)
Output:
Counter: Counter({'red': 3, 'blue': 1, 'green': 1})
itertools.count: [100, 105, 110, 115]
manual dict increment: {'red': 3, 'blue': 1, 'green': 1}
Counter tallies a whole sequence in one call, so there is no loop and no counter to get wrong.
itertools.count is an endless generator for things like invoice numbers. For running totals inside a dictionary, see incrementing a dictionary value.
Python increment and decrement at a glance
| You write | What happens |
|---|---|
i++ | SyntaxError |
i-- | SyntaxError |
++i | Valid, returns i, changes nothing |
--i | Valid, returns i, changes nothing |
i += 1 | Increments by 1 |
i -= 1 | Decrements by 1 |
More Python operator and loop guides:
- Increment a dictionary value
- Python variables explained
- List comprehension with if else
- Recursion in Python
- Fibonacci series in Python
- Fixing NameError in Python
Frequently asked questions
Does Python have an increment operator?
No. Python has no ++ or --. Use x += 1 and x -= 1, which are the augmented assignment operators described in the Python assignment statement reference.
Why does i++ give a SyntaxError in Python?
Because ++ is not an operator in Python. The parser reaches the second + with nothing to add and reports invalid syntax.
Why does ++x work but do nothing?
Python reads it as unary plus applied twice, +(+x). That returns the same value and never assigns it back, so the variable is unchanged.
How do I increment a variable by 1 in Python?
x += 1. For any other step, use that number instead, such as x += 5.
How do I decrement a variable in Python?
x -= 1. To count down in a loop, use range(5, 0, -1).
Why is my while loop infinite when I use ++i?
Because ++i never changes i, so the loop condition stays true forever. Replace it with i += 1.
Do I need a counter variable in a Python for loop?
Usually not. enumerate() gives you the index alongside each item, and range() takes a step argument.
Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn