Python Increment and Decrement Operators

PythonServer Side ProgrammingProgramming

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.

Example

x=786

x=x+1
print(x)

x+=1
print(x)

x=x-1
print(x)

x-=1
print(x)

Output

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

Example

for i in range(0,5):
   print(i)

for i in range(0,5,2):
   print(i)

Output

0
1
2
3
4
0
2
4

Conclusion

In this article, we learned how to use Increment and Decrement Operators in Python.

raja
Published on 07-Aug-2019 10:51:40
Advertisements