Open In App

Increment and Decrement Operators in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

If you’re familiar with Python, you would have known Increment and Decrement operators ( both pre and post) are not allowed in it. Python is designed to be consistent and readable. One common error by a novice programmer in languages with ++ and — operators are mixing up the differences (both in precedence and in return value) between pre and post-increment/decrement operators. Simple increment and decrement operators aren’t needed as much as in other languages. In this article, we will see how to increment in Python as well as decrement in Python.

Python Increment Operator (+=)

In Python, we can achieve incrementing by using Python ‘+=’ operator. This operator adds the value on the right to the variable on the left and assigns the result to the variable. In this section, we will see how use Increment Operator in Python.

We don’t write things like:

for (int i = 0; i < 5; ++i)

For normal usage, instead of i++, if you are increasing the count, you can use

i+=1 or i=i+1

In this example, a variable x is initialized with the value 5. The += operator is then used to increment the variable by 1, and the result is displayed, showcasing a concise way to perform the increment operation in Python.

Python3




# Initializing a variable
x = 5
 
# Incrementing the variable by 1
# Equivalent to x = x + 1
x += 1
 
# Displaying the result
print("Incremented value:", x)


Output

Incremented value: 6


Python Decrement Operator (-=)

We do not have a specific decrement operator in Python (like -- in some other programming languages). However, you can achieve decrementing a variable using the -= operator. This operator subtracts the value on the right from the variable on the left and assigns the result to the variable.

For normal usage, instead of i–, if you are increasing the count, you can use

i-=1 or i=i-1

Python3




# Initializing a variable
x = 10
 
# Decrementing the variable by 1
# Equivalent to x = x - 1
x -= 1
 
# Displaying the result
print("Decremented value:", x)


Output

Decremented value: 9


Decrement and Increment Operator With for loop

In Python, instead, we write it like the below and the syntax is as follows:

Syntax: for variable_name in range(start, stop, step)

Parameters:

  • start: Optional. An integer number specifying at which position to start. Default is 0
  • stop: An integer number specifying at which position to end.
  • step: Optional. An integer number specifying the incrementation. Default is 1

We can adjust start and stop with help of Python decrement and increment operators.

In this example, the Python increment operator (+=) is demonstrated by incrementing the variable count by one. Additionally, the range() function is utilized in a for loop to showcase both incrementing and decrementing loops, providing a Pythonic alternative to traditional increment and decrement operators found in some other programming languages.

Python3




# A sample use of increasing the variable value by one.
count = 0
count += 1
count = count+1
print('The Value of Count is', count)
 
print("INCREMENTED FOR LOOP")
for i in range(0, 5):
    print(i)
 
# this is for increment operator here start = 5,
# stop = -1 and step = -1
print("\n DECREMENTED FOR LOOP")
for i in range(4, -1, -1):
    print(i)


Output

The Value of Count is 2
INCREMENTED FOR LOOP
0
1
2
3
4

 DECREMENTED FOR LOOP
4
3
2
1
0




Last Updated : 11 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads