How to Increment for Loop in Python

Spread the love

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

Related: How to Decrement for Loop in Python

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 in for loop the counter value increment by ‘1’ for every iteration. If we want to increment the counter value with the specified number we can go with the range() function.

1. Quick Examples of Incrementing Python for Loop

Following are quick examples of custom incrementing for loop in Python.


# Below are the quick examples

# Example 1: Default increment in a for loop using range()
for i in range(6):

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

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

# Example 5: Increment the value using slicing
list=[10, 20, 50, 30, 40, 80, 60]
for i in list[0::3]:
    print(i)

# Example 6: Increment for loop using list comprehension
y = 5
for i in ([x/y for x in range(5, 50, 10)]): 
  print(i)

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

2. Syntax of range()

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


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

2.1 Parameters of range()

Following are the parameters of the range() function.

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

2.2 Return Value

It returns a list of series

3. Using range() Increment by 2 in for Loop

Python range() function is used to generate a sequence of numbers within a given range. By default using the range() function in a for loop, the loop will be incremented by ‘1’ for every iteration. Because the default value of step param is 1. For example,


# Default increment in a for loop using range()
for i in range(6):
  print(i)

Yields below output.

4. Python for Loop Increment by 2

If we want to increment for loop with a specified value we can pass step parameter along with start and stop of range() function. It will increment the loop with the specified step value. For example, to increment for loop by 2 in Python you should use the range() with step value 2.


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

Yields below output.

Python For Loop Increment

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

In this example, we iterate the list of values using for loop along with the range() and len() functions and display the values from a list by incrementing 2.


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

# Output:
# 10
# 50
# 40
# 60

6. Increment for Loop using Variable

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


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

# Output:
# 0
# 3
# 6
# 9

7. Increment for Loop using Slicing

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


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

# Output:
# 10
# 30
# 60

8. Increment for Loop using List Comprehension

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


# Increment for loop using list comprehension
y = 5
for i in ([x/y for x in range(5, 50, 10)]): 
  print(i)

# Output:
# 1.0
# 3.0
# 5.0
# 7.0
# 9.0

9. Increment for Loop using Float values

Finally, we can also increment 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.


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

# Output:
# 10.5
# 13.0
# 15.5
# 18.0

10. Conclusion

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

Related Articles

Leave a Reply

You are currently viewing How to Increment for Loop in Python