Python provides the increment operator
+= in order to increase a variable value. The increment operator provides a very easy and practical way to increment variable typing less. Using the increment operator also decreases the error rate with compact usage with less typing.
Increment Operator Syntax
The Python increment operator has the following syntax where the number is the increment value.
VARIABLE += INCREMENT_VALUE
- VARIABLE is summed with the INCREMENT_VALUE and assigned to the VARIABLE again.
Now we will use the increment operator in order to increment the variable named
age one by one.
age = 18 print(age) age += 1 print(age)
Increment Variable by 2
The increment operator can be also used to increment specified variables by 2.
age = 18 print(age) age += 2 print(age)