0

I read a script like that

for ... :
   for ...:
      ++i

but what does ++ mean? Is ++ operator is python?

jamylak
  • 128,818
  • 30
  • 231
  • 230
Ryan_Liu
  • 269
  • 4
  • 9

4 Answers4

10

In python, that's just unary plus twice. It doesn't do anything. A single one might coerce a bool to an int, but the second one is totally useless.

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
6
>>> +1
1
>>> ++1
1
>>> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++1
1
jamylak
  • 128,818
  • 30
  • 231
  • 230
3

You can use i+=1 instead of i++ for your for loop. There is no ++ usage in Python.

Ahmet DAL
  • 4,445
  • 9
  • 47
  • 71
3

Python is an implicitly typed language, therefore, unless we know what type the variable has, we cannot tell for sure what happens if we apply an operator to it. In your example, i is not necessarily an integer, it can be an object with an overloaded unary + (__pos__), for example:

class Duplicator(object):
    def __init__(self, s):
        self.s = s

    def __pos__(self):
        self.s += self.s
        return self

    def __str__(self):
        return self.s

z = Duplicator("ha ")
# 1000 lines of code
print +z
print ++z
print +++z

So the answer to your question "what does ++x mean in python" is "it depends on what x is".

georg
  • 211,518
  • 52
  • 313
  • 390