Coder Perfect

++ increments an integer in Python. [duplicate]


When I think back to my VB6 days and think, “What current language doesn’t enable incrementing with double plus signs?” I always giggle to myself. :


To my surprise, I can’t find anything about this in the Python docs. Must I really subject myself to number = number + 1? Don’t people use the ++ / — notation?

Asked by Znarkus

Solution #1

Although Python does not have ++, you can use it in the following way:

number += 1

Answered by Daniel Stutzbach

Solution #2

Simply explained, the ++ and — operators do not exist in Python since they would have to be statements rather than operators. To keep things simple and consistent, every namespace adjustment in Python is a statement. One of the design decisions was to do so. Because integers are immutable, reassigning a variable is the sole way to ‘alter’ it.

Fortunately, we have fantastic tools in other languages for the use-cases of ++ and –, such as enumerate() and itertools.count ().

Answered by Thomas Wouters

Solution #3

You can do:

number += 1

Answered by knutin

Solution #4

Yes. In Python, the ++ operator is not available. These operators irritate Guido.

Answered by Pierre-Antoine LaFayette

Solution #5

The most common use of ++ in C-like languages is to keep track of indexes. In Python, you work with data in an abstract manner, rarely incrementing using indices or other means. The next method of iterators is the closest thing to ++ in spirit.

Answered by Mike Graham

Post is based on