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.
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.
In this example, we will read a number into x from user, and increment the value in x by one using Addition Assignment operator.
x = int(input('enter a number : ')) x += 1 print('after increment :', x)
enter a number : 4 after increment : 5
In this tutorial of Python Examples, we learned how to increment the value of a variable by one.