Perform For Loop Decrement in Python

How to decrement (for example by 2) for loop in Python? usually, Python for loop is incremented by 1 value however sometimes you would be required to decrement by 1, 2, 3, 4 e.t.c. You can easily achieve this by using the range() function, with the range you can decrement the for loop index with a negative step value.

The for loop is used to iterate over a sequence (such as a list, tuple, or string) and execute a block of code for each item in the sequence. By default the for loop increments by ‘1’ for every iteration. If we want to decrement the counter value with the specified number we can go with the range() function.

1. Quick Examples of using Decrement in for Loop

Following are quick examples of decrementing a counter with for loop.


# Below are the quick examples

# Example 1: Customized decrement in a for loop using range()
for i in range(6, 0, -1):

# Example 2: Decrement for loop by 2
for x in range(6, 0, -2):

# Example 3: Decrement for loop by 2 using len()
list=[10, 20, 50, 30, 40, 80, 60]
for x in range(len(list)-1, -1, -2):
    print(list[x])

# Example 4: Initialize the decrement value with integer
x = -3
for i in range(10, 0, x):

# Example 5: Decrement for loop using reversed()
for i in reversed(range(1,6)):

# Example 6: Decrement the value using slicing
list=[10, 20, 50, 30, 40, 80, 60]
for i in list[-1::-3]:

# Example 7: Decrement for loop using list comprehension
y = 1
for i in ([x*y for x in range(50, 10, -10)]): 

# Example 8: Decrement the float values in a for loop
import numpy as np
for i in np.arange(20.5, 10.5, -2.5):

2. Syntax of range()

Following is the syntax of the range() function. From the params, we use a step param with a customs value in for loop to decrement.


# Syntax of range() function
range(start, stop, step)

Following are the parameters of the range() function. It returns a list of series

  • start : The starting point of the series.
  • stop : The ending point of the series.
  • step : Specifies how to decrement the value.

3. Using range() Decrement for Loop

Python range() function is used to generate a sequence of numbers within a given range, to decrement the loop we can customize the step value with a specified negative value. For example,


# Customized decrement in a for loop using range()
for i in range(6, 0, -1):
  print(i)

Yields below output.

Python For Loop Decrement

As we can see from the above the numbers we have got 6, 5, 4, 3, 2, and 1 in decrement order. Here, the start argument of the range() function starts from the value 6, the stop argument ends with 0 and the third argument step is set with -1.

4. Python for Loop Decrement by 2

When we want to decrement for loop by 2 we have to specify the step value of the range() function with -2. It will decrement the loop with the specified step value. For example,


# Decrement for loop by 2
for x in range(6, 0, -2):
    print(x)

Yields below output.

Python for loop decrement

5. Python for Loop Decrement by 2 using len()

When you wanted to iterate the list in reverse order by decrementing counter 2, use the len() to get the length of the list and use it as the start value and use the -2 as the step value.


# Decrement for loop by 2 using len()
list=[10, 20, 50, 30, 40, 80, 60]
for x in range(len(list)-1, -1, -2):
    print(list[x])

# Output:
# 60
# 40
# 50
# 10

6. Decrement for Loop using Variable

Use the specified integer variable assigned with the decrement value in a for loop using the range() function. Passing the negative integer value into range() as a step parameter it will decrement the for loop with a specified negative integer.


# Initialize the decrement value with integer
x = -3
for i in range(10, 0, x):
  print(i)

# Output:
# 10
# 7
# 4
# 1

7. Decrement a for Loop in Python Using the reversed()

Alternatively, we can use reversed() function to decrement the for loop. Pass range() function into reversed() function(will reverse the range() function without any modification) and it using for loop. This syntax will return the values in decrement order.


# Decrement for loop using reversed()
for i in reversed(range(1,6)):
    print(i)

# Output:
# 5
# 4
# 3
# 2
# 1

8. Decrement for loop using Slicing

Python slicing is a useful technique when we want to get a specified step of decrement in for loop. Let’s apply the slicing technique with for loop by decrementing 3.


# Decrement the value using slicing
list=[10, 20, 50, 30, 40, 80, 60]
for i in list[-1::-3]:
    print(i)

# Output:
# 60
# 30
# 10

9. Increment For loop using List Comprehension

Using list comprehension we can decrement inside the loop. Here, I will decrement the values in a for loop using list comprehension.


# Decrement for loop using list comprehension
y = 1
for i in ([x*y for x in range(50, 10, -10)]): 
  print(i)

# Output:
# 50
# 40
# 30
# 20

10. Increment For loop using Float values

Finally, we can also decrement for loop with float values. For float values, we have to use numpy.arange() functions with start, stop and step value. Before going to use np.arange() we have to import the numpy module as an np.


# Decrement the float values in a for loop
import numpy as np
for i in np.arange(20.5, 10.5, -2.5):
    print (i)

# Output:
# 20.5
# 18.0
# 15.5
# 13.0

11. Conclusion

In this article, I have explained the Python range() function and how we can decrement the for loop by custom values like 2, 3, etc. To perform the decrement you have to use the step param with the value you wanted to decrement.

Related Articles

Vijetha

With 5 of experience in technical writing, I have had the privilege to work with a diverse range of technologies like Python, Pandas, NumPy and R. During this time, I have consistently demonstrated my ability to grasp intricate technical details and transform them into comprehensible materials.

Leave a Reply

You are currently viewing Perform For Loop Decrement in Python