The increment and decrement operators have well-known built-in operations in most programming languages like C/C++. But in Python, there is no such accessible postfix/prefix increment and decrement operator. However, sometimes, the programmer wants to operate on building patterns or designing a particular program. In such scenarios, as an alternative approach, the coder can implement operations like looping and other Python built-in arithmetic operators to perform the increment and decrement operation.  

This article is about exploring the behavior of increment and decrement operators in Python. To explore the behaviour of increment and decrement operators in Python, go through the below-listed topics:

  • Behaviour of Increment and Decrement Operators in Python?
  • Check the Behaviour of the Increment Operator in Python
  • Behaviour of Decrement Operator in Python 
  • Implementing Increment and Decrement Operator in Python by Nested Looping through Range
  • Implementing Increment and Decrement Operator in Python Using Nested While Loop
  • Bonus Tip: Check the Behaviour of Increment Operators by Assignment Operator (=)

Behaviour of Increment and Decrement Operators in Python?

The increment and decrement operators have a particular syntax followed by many other languages (like C++/C) but in Python language, there is no proper syntax or operator allocated for performing the increment and decrement operation particularly. However, Python gives flexible access to programmers to execute the increment and decrement operators by utilizing the “assignment operator” (=) along with primary “arithmetic operators” (+/-). 

First, check the behaviour of increment operators in Python: 

Approach 1: Check the Behaviour of the Increment Operator in Python 

The increment operator will increase the value of the variable by some defined step value and increments until and unless the termination of the program executes. To check the behaviour of the increment operator in Python, follow the below-listed set of example codes:

  • Implementing increment Operator in Python By Defining User-Defined Function
  • Implementing increment Operation in Python By Arthematic Operator 

Example 1: Implementing Increment Operator in Python by Defining User-Defined Function

Within the code body of the defined function, the increment operation is performed within the return statement implicitly. First, define a function with the keyword “def”, and perform the increment operation within the code body. After that invoke the user-defined function with argument value. For demonstration, follow the below example code:

#define a user-defined function using 'def' keyword
def count (j):
    for j in range (0,5):
             print (f'{5} * {j} =', j*5)
#print the results
count(5)

The above example code depicts the increment behaviour of the program. In this program to check the increment behaviour in Python, the function is defined implicitly: 

Output 

Example 2: Implementing Increment Operation in Python by Arthematic Operator 

As mentioned above, there is no such syntax followed by Python language for incrementing and decrementing the values. To implement or check the behaviour of increment operators in Python, use the “augmented assignment operators”. To implement the augmented operator, utilize the primary arithmetic plus (+) operator with aggregation of the assignment operator (=). For demonstration, follow the below example code:  

# Import sleep function from the time library
from time import sleep

# define a variable with the value
Stopwatch = float(0.00)

# executing the while () function 
while Stopwatch<float(11.00):
    
     # pauses the screen for one second each time the loop iterate and update the results
    sleep(1)  
    
    # printing the results 
    print ("Time Left : ", Stopwatch)

    # increment the value by one step every time the loop iterates 
    Stopwatch += 1

In the above example, the objective is to execute the stopwatch from 0 to 10, and after the value 10 is executed, terminate the program. For this purpose, the while loop is utilized. The while loop executes the operation until and unless the code body condition is true. The “sleep ()” function pauses the output upon each iteration. The augmented assignment operator “(+=)” is utilized to increment the value.

Note: In the code block of the while loop, after providing the condition, don’t forget to embed the “:” colon to give justice to the Python syntax. 

Output

Each time the loop iterates the value is printed on the console by incrementing the previous value and iterates until the condition is satisfied, after that the program terminates: 

Approach 2: Behaviour of Decrement Operator in Python 

The decrement operator will decrease the value of the object by some step value. To perform the decrement operation in Python, the user can utilize the augmented assignment operator and the other user-defined functions. 

To check the decrement operator in Python, utilize the below-listed example codes:

  • Implemented Decrement Operation in Python through Looping
  • Decrement in Python Using the User Define Function in Python
  • Decrementing in Python Using the Augmented Assignment Operator

Example 1: Implemented Decrement Operation in Python through Looping 

The decrement operator in Python is executed using looping over the range function. Within the range function, the starting index must be greater than the stop index to execute the iteration of the sequence: 

print("Table of 5 ")

#using range() by defining decrement step 
for i in range (10,0,-1):

    #utilizing string literals to format the output
    print (f'{5} * {i} =', i*5)

In the above example code, the alternative approach is implemented to increment the sequence through the “range()” function via looping. The range function takes three arguments the start Index, the stop Index, and the decrement operation by one. 

Output

The decrement operation is performed in Python through looping, as illustrated below snap: 

Example 2: Decrement in Python Using the User Define Function in Python 

The user-defined function is another alternative approach to checking the behaviour of the decrement operator in Python. To perform the decrement operation in Python, the function is defined using the “def” operator. Within the defined function code body the decrement operation is performed:

#define a user-defined function using the 'def' keyword
def count (j):
    for j in reversed(range(5)):
        
    #using f-string to format output 
             print (f'{5} * {j} =', j*5)
             
#print the results
count(5)

To perform the decrement operation, the “reversed()” function with aggregation with the “range()” function is utilized. The iteration sequence is defined within the range function parenthesis. However, the function is defined implicitly without the return statement. If there is no program within the function, it implicitly returns “None” on the console.  

Output

The output illustrates the decrementing behavior in Python. 

Example 3: Decrementing in Python Using the Augmented Assignment Operator 

The decrement operation is implemented using the while loop. The program is performed within the loop body using the augmented operator for the decrement operation:

import time 
Stopwatch = float(10.00)
#using the while loop to execute the decrementing behaviour in Python

while Stopwatch>float(0.00):

    print ("Time Left : ", Stopwatch)

    #invoking sleep function
    time.sleep(1)

    #decrement the value on each iteration 
    Stopwatch -= 1

The above example code demonstrates the decrementing behaviour in Python. However, the “augmented assignment operator (-=) is implemented to acquire the decrementing behaviour in Python. 

Output

The output performed the decrement operation using the augmented assignment operator: 

Approach 3: Implementing Increment and Decrement Operator in Python by Nested Looping through Range

The for loop gives flexible access to users to increment or decrement the values in aggregation with the “range()” function. The for loop is used to iterate over the iterable such as sequence. The number of sequences is defined in the range function “()” parentheses. The behaviour of increment and decrement operators in Python is demonstrated in the below example code: 

print('increment operation in Python')

#print table of 5
print("\nTable of 5 ")

#using for loop for executing iteration over a range() iterable 
for i in range (0,5):
    print (f'{5} * {i} =', i*5)

print('\ndecrement operation in Python\n')

for i in range(1):
    print("Decrementing Loop:")
    for j in range(5,0,-1):
    #using f-string to format output 
        print (f'{5} * {j} =', j*5)

The above block demonstrates that the looping through the range function will repeat the code block each time the sequence in the range() function executes. For decrement, the nested for loop is executed, and the outer for loop executes the sequence for one time as the range defined in the range function is one “range(1)”, and the inner for loop will perform the decrement operation with the help of the sequence defined in range() function.

Output

The increment and decrement operation is performed through the for loop using the range() function:

Approach 4: Implementing Increment and Decrement Operator in Python Using Nested While Loop

The nested while loop can be utilized to show the increment and decrement behavior in Python. To do so, the outer and inner loop combination of the while loop is executed in such a way as to execute the incrementing and decrementing in Python:

# Import sleep function from the time library
from time import sleep


# While loop to check decrement behaviour
decrement_counter = 1
count = 12

while decrement_counter >= 1:
    print("Decrementing Loop:")
    decrement_counter -= 1

    while count >= 1:
        #logic for decrement in Python
        sleep(1)  
        count -= 2
        print(f" count = {count}")

  
increment_counter = 1
count = 0

# While loop to check Increment behaviour
while increment_counter <= 1:
    print("Incrementing Loop:")
    increment_counter += 1
    while count <= 12:
        #logic for increment in Python
        sleep(1)  
        count += 2
        print(f" count = {count}")

In the above example code, the outer loop prints the simple string statement, and the inner while loop executes the increment and decrement logic. To increment the “augmented assignment” operator (+=) is utilized and to execute the decrement operation the augmented decrement operator (-=) is implemented. 

Output

The below output shows the increment and decrement behavior in Python using nested While loop:

Bonus Tip: Check the Behaviour of Increment Operators by Assignment Operator (=)

In C++/C incrementing the three variables using the increment/decrement operator (++/–) raises the error. But Python language gives flexible access to allocate the same memory to the three variables using the assignment operator. However, there is no increment or decrement operator in Python, but the assignment operator (=) is utilized to get the incremented or decremented results.  

This article demonstrates the behavior of increment and decrement operators in Python. 

Conclusion

To check the behaviour of increment and decrement operators in Python, there is no such operator postfix/prefix operator to execute the increment and decrement operation. However, alternatively, the programmer can implement the increment and decrement operation by performing the looping operations in Python. This article is all about checking the increment and decrement operators in Python.