In this article, we will learn about increment and decrement operators in Python 3.x. Or earlier. In other languages we have pre and post increment and decrement (++ --) operators.
In Python we don’t have any such operators . But we can implement these operators in the form as dicussed in example below.
x=786 x=x+1 print(x) x+=1 print(x) x=x-1 print(x) x-=1 print(x)
787 788 787 786
Other languages have for loops which uses increment and decrement operators. Python offers for loop which has a range function having default increment value “1” set. We can also specify our increment count as a third argument in the range function
for i in range(0,5): print(i) for i in range(0,5,2): print(i)
0 1 2 3 4 0 2 4
In this article, we learned how to use Increment and Decrement Operators in Python.