Open In App

Increment += and Decrement -= Assignment Operators in Python

Last Updated : 30 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
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




Previous Article
Next Article

Similar Reads

Augmented Assignment Operators in Python
An assignment operator is an operator that is used to assign some value to a variable. Like normally in Python, we write "a = 5" to assign value 5 to variable 'a'. Augmented assignment operators have a special role to play in Python programming. It basically combines the functioning of the arithmetic or bitwise operator with the assignment operator
4 min read
Assignment Operators in Python
The Python Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic, logical, and bitwise computations. The value the operator operates on is known as the Operand. Here, we will cover Different Assignment operators in Python. Operators Sign Description SyntaxAssignment Operator = Assi
9 min read
Python - Decrement Dictionary value by K
Sometimes, while working with dictionaries, we can have a use-case in which we require to decrement a particular key’s value by K in dictionary. It may seem a quite straight forward problem, but catch comes when the existence of a key is not known, hence becomes a 2 step process at times. Let’s discuss certain ways in which this task can be perform
3 min read
Decrement in While Loop in Python
A loop is an iterative control structure capable of directing the flow of the program based on the authenticity of a condition. Such structures are required for the automation of tasks. There are 2 types of loops presenting the Python programming language, which are: for loopwhile loop This article will see how to decrement in while loop in Python.
3 min read
How to Decrement a Python for Loop
Python's for loop is a fundamental construct used for iterating over sequences. While Python does not have a built-in feature to decrement a loop index directly, there are multiple approaches to achieve this functionality. In this article, we'll explore different methods to decrement a for loop in Python, each with its advantages and use cases. Dec
2 min read
A += B Assignment Riddle in Python
Predict the output of these two expressions on Python console On console Geek = (1, 2, [8, 9]) Geek[2] += [3, 4] Output: Explanation: Look at the bytecode Python generates for the expression s[a] += b. It becomes clear how that happens. It works step by step Put the value of s[a] on Top Of Stack(TOS). Perform TOS += b. This succeeds, If TOS refers
1 min read
Python | Priority key assignment in dictionary
Sometimes, while working with dictionaries, we have an application in which we need to assign a variable with a single value that would be from any of given keys, whichever occurs first in priority. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop This task can be performed in a brute force manner using loop. I
4 min read
Python - Maximum value assignment in Nested Dictionary
Sometimes, while working with Python dictionaries, we can have a problem in which we need to assign to the outer key, the item with maximum value in inner keys. This kind of problem can occur in day-day programming and web development domains. Let's discuss a way in which this task can be performed. Input : test_dict = {'Manjeet': {'English': 19, '
3 min read
Different Forms of Assignment Statements in Python
We use Python assignment statements to assign objects to names. The target of an assignment statement is written on the left side of the equal sign (=), and the object on the right can be an arbitrary expression that computes an object. There are some important properties of assignment in Python :- Assignment creates object references instead of co
3 min read
Python - Sequence Assignment to Words
Given a String of words, assign an index to each word. Input : test_str = 'geeksforgeeks is best' Output : {0: 'geeksforgeeks', 1: 'is', 2: 'best'} Explanation : Index assigned to each word. Input : test_str = 'geeksforgeeks best' Output : {0: 'geeksforgeeks', 1: 'best'} Explanation : Index assigned to each word. Method #1: Using enumerate() + dict
5 min read
Article Tags :
Practice Tags :