988

I've always laughed to myself when I've looked back at my VB6 days and thought, "What modern language doesn't allow incrementing with double plus signs?":

number++

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?

NerdOnTour
  • 634
  • 4
  • 15
Markus Hedlund
  • 23,374
  • 22
  • 80
  • 109
  • 10
    I for one am quite happy that we don't have to put up with things like `a[i] = i++;` where the order of evaluation in C++ is undefined. – Tim Pietzcker Apr 13 '10 at 19:51
  • 3
    Even if there's such a thing, I think in Python the order is well-defined. (http://docs.python.org/reference/expressions.html#evaluation-order) – kennytm Apr 13 '10 at 20:16
  • 5
    Answers to your questions in the given order: “Erlang, Python, Lua etc” (for "modern" meaning after the creation of C); “No”; and “Not necessarily”. – tzot Apr 13 '10 at 22:04
  • Are you talking about prefix `++` or postfix `++`? I, for one, hated teaching this part of the C language, and avoided it because of the ambiguities. Why look for this horrible thing in other languages? – S.Lott Apr 14 '10 at 02:43
  • 4
    Indeed, why to have the shortcuts for pervasive task? – Val Oct 29 '13 at 11:37
  • 77
    Disagree with y'all: `i++` is less to write so less to read. Less to read means brain can focus more on the big picture. – Robino Feb 05 '16 at 15:26
  • 10
    @TimPietzcker: a better solution would be to well define the evaluation order, probably from left to right, rather than dropping a useful operator. And to the OP: Python is hardly a modern language... and is a quite crappy language actually, despite being widely used. – Yakov Galka Jun 27 '16 at 13:43
  • 2
    @Robino Less to read, more to process. What's more complex and ugly, `i++` or `i += 1`? – noɥʇʎԀʎzɐɹƆ Jul 12 '16 at 21:11
  • 5
    @uoɥʇʎPʎzɐɹC Neither is particularly complex and neither is ugly. The first one is quicker to understand. – Robino Jul 20 '16 at 10:19
  • @TimPietzcker Python is hardly a crappy or archaic language. It has a fairly unique syntax and is easy to learn. If by crappy you mean slow because it is interpreted rather than compiled, pyc is actually very quick. – chevydog Feb 01 '17 at 00:43
  • ++i vs i++, then let's talk about "brain can focus on other things" again, @Robino. – Jürgen A. Erhard Mar 18 '19 at 21:00
  • Learning python myself now (C# background) and I find this annoying. I would say it is smarter to just implement it and let the programmer choose how they want to write their syntax. Frankly there is nothing ambiguous or hard to understand about i++. The people using ++i without understanding what it does - that's their fault. I just don't think taking away such a common statement is fair though, but whatever it is what it is. – dyslexicanaboko Jul 26 '19 at 01:17
  • 3
    @JürgenA.Erhard `++i` = i is incremented then returned. `i++` = i is returned then incremented. Seems pretty simple to me... Yes, a brand new person who has never seen it before might not know that, but it doesn't take much to figure it out (a simple Google/SO search), and the flexibility provided is quite handy in some situations. I am less liberal with `i++` than some (I don't usually use things like `result = array[i++]` for example). @others I do agree it is quicker to see what is going on than with `i += 1`. There is less for my brain to parse and it just 'clicks' as soon as I see it. – RTHarston Nov 14 '19 at 17:08
  • What's even more amazing is that in 2022 they still did not add '++' to the language :-( As for readability, if you are not used to '++' it is obviously less readable than x += 1 (like for some people not used to the ternary operator, it's an "awful feature"...) – Déjà vu Jan 03 '22 at 07:36
  • is there a reason for still not having `++` in python? – ArieAI Jan 18 '23 at 14:42

7 Answers7

1723

Python doesn't support ++, but you can do:

number += 1
Daniel Stutzbach
  • 74,198
  • 17
  • 88
  • 77
  • 119
    I think that @Thomas's explanation is more useful here; I think the question is more of `why` and not `what`. – rickcnagy Apr 07 '14 at 21:43
  • 1
    Agreed with @rickcnagy, more like the "how to do it?" (if you really don't care about brevity of code you could also simply do number = number + 1) the reasoning on why ++ and -- don't exist in Python seems more useful. – alezvic May 03 '17 at 15:40
  • 7
    Not exactly. The following will not work as expected: `progress = 0; print(progress += 1)`. So `+=` does not seem to completely replace C++'s `++` operator. – Dr_Zaszuś Oct 12 '18 at 18:07
  • Python has loads of ?= where ? is replaced by another operator, although it wont work with every operator. For example n \= 2 becomes n = n \ 2, however n +== 1 doesn't unpack to n = n += 1 – alan2here Feb 27 '21 at 18:41
531

Simply put, the ++ and -- operators don't exist in Python because they wouldn't be operators, they would have to be statements. All namespace modification in Python is a statement, for simplicity and consistency. That's one of the design decisions. And because integers are immutable, the only way to 'change' a variable is by reassigning it.

Fortunately we have wonderful tools for the use-cases of ++ and -- in other languages, like enumerate() and itertools.count().

Thomas Wouters
  • 130,178
  • 23
  • 148
  • 122
  • 22
    useful reference to `enumerate()` and `itertools.count()` – tato Jun 08 '15 at 04:28
  • 2
    Care to suggest an elegant replacement for this: `reserved_index = 0; col_names = [name if name != '_' else 'reserved' + (reserved_index++) for name in column_names]`? I'm passed a list of column names where some that are not interesting are just `'_'`. I need to construct a temporary table with those `'_'`s replaced with unique but non-meaningful names. The in-place postincrement operator would make this easy; I'm struggling to come up with something else that doesn't involve looping over the array explicitly. – Tom Dec 07 '16 at 11:40
  • 6
    @Tom `reserved_indices = itertools.count(); col_names = [name if name != '_' else 'reserved' + str(next(reserved_indices)) for name in column_names]` – Artyer Aug 02 '17 at 23:58
  • 5
    This decision violates the rule - no surprise. ++number works in most of languages. - How many bugs this may cause. The language should be also developer friendly ) – jeffery.yuan Mar 08 '18 at 21:28
  • 2
    @jeffery.yuan Been at it less than a year (meaning python--I've coded for a couple decades plus), but, man, the number of things like that...boggles the mind. Lowest common denominator kinda won. – msouth Sep 05 '19 at 19:33
  • PEP 572 changed much of this. – Gringo Suave Mar 06 '20 at 02:46
  • [PEP 572](https://www.python.org/dev/peps/pep-0572) currently changes nothing for most of us, because assignment expressions are only available under Python 3.8. Meanwhile, Python 3.5 through 3.7 have yet hit to EOL. If you'd like your code to remain compatible with actively maintained Python versions (and you should), just use the [long-standing `itertools.count()` iterator](https://docs.python.org/3/library/itertools.html#itertools.count), which has existed since *literally the dawn of time, space, and Python 3 itself,* is trivial to use, and comes with no such portability concerns. – Cecil Curry Aug 14 '20 at 06:01
  • 1
    are = you[kidding++] << me; are = you[plus(kidding)] < – Christian Lacdael Oct 13 '20 at 15:09
63

You can do:

number += 1
knutin
  • 5,033
  • 19
  • 26
37

Yes. The ++ operator is not available in Python. Guido doesn't like these operators.

Pierre-Antoine LaFayette
  • 24,222
  • 8
  • 54
  • 58
26

The main reason ++ comes in handy in C-like languages is for keeping track of indices. In Python, you deal with data in an abstract way and seldom increment through indices and such. The closest-in-spirit thing to ++ is the next method of iterators.

Mike Graham
  • 73,987
  • 14
  • 101
  • 130
  • 2
    Sometimes I find that you really do just need iteration indexes, e.g., if you want to track how many times a function runs before it converges, etc. Though maybe that still counts as "seldom used", Python is pretty well suited for the most part to scientific coding, FWIW. – jrh Dec 21 '18 at 16:21
12

Take a look at Behaviour of increment and decrement operators in Python for an explanation of why this doesn't work.

Python doesn't really have ++ and --, and I personally never felt it was such a loss.

I prefer functions with clear names to operators with non-always clear semantics (hence the classic interview question about ++x vs. x++ and the difficulties of overloading it). I've also never been a huge fan of what post-incrementation does for readability.

You could always define some wrapper class (like accumulator) with clear increment semantics, and then do something like x.increment() or x.incrementAndReturnPrev()

Community
  • 1
  • 1
Uri
  • 88,451
  • 51
  • 221
  • 321
9

Here there is an explanation: http://bytes.com/topic/python/answers/444733-why-there-no-post-pre-increment-operator-python

However the absence of this operator is in the python philosophy increases consistency and avoids implicitness.

In addition, this kind of increments are not widely used in python code because python have a strong implementation of the iterator pattern plus the function enumerate.

pygabriel
  • 9,840
  • 4
  • 41
  • 54