Decrement by One
Decrement by One
To decrement the value of a variable by one in Python, we can use Subtraction Assignment operator, and pass the variable and value 1 as operands to it.
Syntax
The syntax to decrement 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 decrement the value of a variable by one.
Example
In this example, we will read a number into x
from user, and decrement the value in x
by one using Subtraction Assignment operator.
Python Program
x = int(input('enter a number : '))
x -= 1
print('after decrement :', x)
Output
enter a number : 4
after decrement : 3
Summary
In this tutorial of Python Examples, we learned how to decrement the value of a variable by one.