You are currently viewing How to Increment For Loop in Python

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.

Advertisements

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

If you are in a hurry, below are some quick examples of custom incrementing for loop in Python.


# Quick examples of incrementing python for loop

# Example 1: Default increment in a for loop 
# Using range() function
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() function
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 (optional) – The starting value of the sequence. If not specified, it defaults to 0.The starting point of the series.
  • stop – The end value of the sequence. The stop value is exclusive, meaning that the sequence will not include this value. The ending point of the series.
  • step(optional) – The increment between each number in the sequence. If not specified, it defaults to 1. 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.

In the below example, the for loop is using the range(6) expression, which generates a sequence of numbers from 0 to 5 (inclusive) with a default increment of 1. This loop will iterate over the sequence of numbers [0, 1, 2, 3, 4, 5], and in each iteration, the variable i will take on one of these values. The print(i) statement will then display the current value of i.


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

Yields below output.

python for loop increment

4. Python For Loop Increment by 2

If you want to increment the loop variable by 2 using the range() function in a for loop, you can specify the step value as the third argument.

In the below example, range(0, 6, 2) generates a sequence of numbers starting from 0, up to (but not including) 6, with a step of 2. The loop variable i takes on the values 0, 2, and 4 in each iteration of the loop, and these values are printed.


# 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()

To use the len() function to determine the length of a sequence and then use a for loop to increment the loop variable by 2, you can achieve this with the range() function and the length of the sequence.

In this example, len(list) returns the length of the list list, and range(0, len(list), 2) generates a sequence of indices starting from 0, up to (but not including) the length of the list, with a step of 2. The loop variable x takes on these indices, and list[x] is printed.


# 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.

You’ve initialized the increment value x to 3, and then you are using it in the range() function to increment the loop variable i by 3 in each iteration. The range(0, 10, x) generates a sequence starting from 0, up to (but not including) 10, with a step of 3. Therefore, the loop variable i takes on the values 0, 3, 6, and 9 in each iteration, and these values are printed.


# 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

Using slicing to iterate over every third element in the list. For instance, list[0::3] create a new list that starts from the first element (index 0) and goes up to the end of the list, with a step of 3. So, it selects every third element in the original list. The loop variable i takes on these values, and they are printed in each iteration.


# 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

You can use list comprehension to create a list of values where each value is obtained by dividing the corresponding value from the range by y.

In the below example, the list comprehension [x / y for x in range(5, 50, 10)] generates a list of values by dividing each element x from the range range(5, 50, 10) by y. The for loop then iterates over this list, and i takes on each value in the list in each iteration, printing the result.


# 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, you can also increment for loop with float values. For float values, you have to use numpy.arange() functions with start, stop and step value. Before going to use np.arange() you have to import the NumPy module as an np.

Using NumPy’s arange function to generate a sequence of float values and then iterating over that sequence in a for loop. The np.arange(10.5, 20.5, 2.5) function call generates a NumPy array with float values starting from 10.5, incrementing by 2.5, and stopping before 20.5. The for loop then iterates over this array, and i takes on each float value in each iteration.


# 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

Frequently Asked Questions On Increment For Loop in Python

How do I increment a for loop in Python using a specific step value?

To increment a for loop in Python with a specific step value, you can use the range() function. The range() function allows you to specify the start, stop, and step values for the loop

Can I increment a for loop with a variable?

You can increment a for loop with a variable. The variable can represent the step value in the loop. For example, the range(start,stop,increment) generates a sequence of numbers starting from start, up to (but not including) stop, with a step of increment. The loop variable i takes on these values, and they are printed in each iteration.

Is it possible to increment a for loop using list comprehension?

While a standard for loop is typically used for iteration, list comprehension is a concise way to create lists, and it can involve an implicit loop. However, list comprehension itself doesn’t directly modify the behavior of a for loop. You use list comprehension to generate a new list based on an existing iterable.

Is it possible to increment a for loop using list comprehension?

In Python, list comprehensions are generally used for creating lists, not for altering the behavior of a loop. List comprehensions are a concise way to generate lists based on existing iterables. While you can use list comprehension to create a list with values derived from a loop, it doesn’t directly control the iteration process or modify the behavior of a for loop.

How can I increment a for loop based on the length of a sequence?

To increment a for loop based on the length of a sequence, you can use the len() function to get the length of the sequence and then use the obtained length in the range of the for loop.

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