Table of Contents
We often use a for loop in python to iterate over a container object like a list or tuple. We also use for loops to perform tasks a fixed number of times. In python, the iterator or value in a for loop increments by one by default. In this article, we will see how we can increment for loop by 2 in Python.
Python For Loop Increment By 2 Using the range() Function
We use the range()
function to implement a for loop in python. The range()
function is first used to create a sequence and then we execute the for loop using the sequence. The syntax for the range()
function is as follows.
1 2 3 |
range(start:end:step) |
Here,
start
is the number at which the output sequence starts. It is an optional parameter.end
is the number at which the output sequence terminates. The output sequence contains numbers only till end-1. It is a mandatory parameter.step
is the common difference between the consecutive numbers in the sequence. It is an optional parameter.
As the parameters start
and step
are optional. We can also use the range()
function as follows.
1 2 3 4 |
range(start:end) range(end) |
Here, whenever start
is not defined, the default value for start
is taken to be 0. If step
is not defined, the default value is taken to be 1.
To increment a for loop by 2, we just have to give the value 2 as the step
parameter as follows.
1 2 3 4 |
for number in range(1, 21, 2): print(number, end=" ") |
Output:
1 2 3 |
1 3 5 7 9 11 13 15 17 19 |
Here, you can observe that we printed a sequence of 10 numbers from 1 to 20 using the range()
function. As we have specified the step
parameter as 2, the for loop increments by 2 because the range()
function returns a sequence having a difference of 2 between the consecutive elements.
Further reading:
Python For Loop Increment By 2 Using List Slicing
In python, we normally iterate through a list directly as follows.
1 2 3 4 5 |
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for element in myList: print(element, end=" ") |
Output:
1 2 3 |
1 2 3 4 5 6 7 8 9 10 |
In this approach, we can access consecutive elements from the list. What if we had to increment the iterator by 2? In such cases, we can use slicing. The syntax for slicing a list is as follows.
newList= myList[startIndex, endIndex,step]
Here,
myList
is the input list andnewList
is the list created by slicingmyList
.startIndex
is the index of the element inmyList
from which we start including the elements in thenewList
. Here, you can leave the startIndex empty if you want to include elements right from the start.endIndex
is the index of the element inmyList
at which we stop including elements ofmyList
innewList
. Here, you can leave theendIndex
empty if you want to include elements till last.step
denotes the number of elements that we skip inmyList
before including the next element tonewList
.
To increment the iterator of the for loop by 2 while iterating a list, we can specify the step as 2 using slicing as follows.
1 2 3 4 5 6 7 8 9 |
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print("Elements in the list are:") for element in myList: print(element, end=" ") print("\nNow printing elements at interval of 2") for element in myList[::2]: print(element, end=" ") |
Output:
1 2 3 4 5 6 |
Elements in the list are: 1 2 3 4 5 6 7 8 9 10 Now printing elements at interval of 2 1 3 5 7 9 |
Here, you can observe that we have first printed all the elements of a list. Then, we have created a slice of the original list to increment the for loop by 2 elements.
For iterating a list, I would suggest you not use the slicing approach. This is so because the sliced list also requires space. So, for larger lists, it might increase the use of memory space. Alternatively, you can use the range()
function and indexing to access the elements at an interval of 2 from the original list as follows.
1 2 3 4 5 6 7 8 9 10 |
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] length = len(myList) print("Elements in the list are:") for counter in range(length): print(myList[counter], end=" ") print("\nNow printing elements at interval of 2") for counter in range(0, length, 2): print(myList[counter], end=" ") |
Output:
1 2 3 4 5 6 |
Elements in the list are: 1 2 3 4 5 6 7 8 9 10 Now printing elements at interval of 2 1 3 5 7 9 |
Conclusion
In this article, we have discussed two ways to increment a for loop by 2 in python. We have also seen why we should prefer the approach that uses the range()
function instead of the slicing method.
I hope you had fun reading this blog. Stay tuned for more informative articles.
Happy Learning.