Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: ++ operator

I often require doding a simple 1 unit incrementation (++) in Python.

I never seem to find a better solution than

x = x + 1

What am I doing wrong ?

like image 672
Calin Paul Alexandru Avatar asked May 29 '12 18:05

Calin Paul Alexandru


People also ask

What does &= mean in Python?

It means bitwise AND operation. Example : x = 5 x &= 3 #which is similar to x = x & 3 print(x)

What is the OR operator in Python?

In short, the Python or operator returns the first object that evaluates to true or the last object in the expression, regardless of its truth value. In this example, the Python or operator returns the first true operand it finds, or the last one. This is the rule of thumb to memorize how or works in Python.

What are the 3 operators in Python?

Python divides the operators in the following groups: Arithmetic operators. Assignment operators. Comparison operators.


2 Answers

Python doesn't have a ++ operator. You should use the += operator:

x += 1
like image 95
Mark Byers Avatar answered Oct 14 '22 23:10

Mark Byers


The answer you are looking for is:

x += 1
like image 25
Lyn Headley Avatar answered Oct 15 '22 00:10

Lyn Headley