Increment by One
Increment by One
To increment the value of a variable by one in Python, we can use Addition Assignment operator, and pass the variable and value 1 as operands to it.
Syntax
The syntax to increment the value of x
by one is
x += 1
This is just a short hand for the following assignment statement.
x = x + 1
We can use the above syntax as well to increment the value of a variable by one.
Example
In this example, we will read a number into x
from user, and increment the value in x
by one using Addition Assignment operator.
Python Program
x = int(input('enter a number : '))
x += 1
print('after increment :', x)
Output
enter a number : 4
after increment : 5
Summary
In this tutorial of Python Examples, we learned how to increment the value of a variable by one.