PDA

View Full Version : [SOLVED] Python variable count



DamjanDimitrioski
August 22nd, 2007, 12:37 PM
In almost all the programming languages, the command ++ will make some variable to have +1 value.
Now, how can i use that in Python, instead of g = g+1?

Nekiruhs
August 22nd, 2007, 12:40 PM
g += 1

DamjanDimitrioski
August 22nd, 2007, 12:50 PM
Sorry i can't understand it, please give me an example!

LaRoza
August 22nd, 2007, 12:52 PM
Sorry i can't understand it, please give me an example!

These snippets do the same thing:



while (x < 5)
{
++ x;
}



In Python:


while (x <5):
+(+x)

Also, you can use
x+=1

The above shorthand is common to increment number by any number, with most operations, so

x = x + 1 == x += 1

x = x *2 == x *= 2

Most languages have this, like C++, Python, and other modern languages.

DamjanDimitrioski
August 22nd, 2007, 12:57 PM
Thanks, i will try it now.

pmasiar
August 22nd, 2007, 02:44 PM
Python does not have ++x, because it is against "explicit is better than implicit" rule: how much you want to increase x? So you need to say explicitly: add one: "x += 1"