For Loop Increment By 2 in Python

Python for loop increment by 2

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.

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.

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.

Output:

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.

Python For Loop Increment By 2 Using List Slicing

In python, we normally iterate through a list directly as follows.

Output:

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 and newList is the list created by slicing myList.
  • startIndex is the index of the element in myList from which we start including the elements in the newList. Here, you can leave the startIndex empty if you want to include elements right from the start.
  • endIndex is the index of the element in myList at which we stop including elements of myList in newList. Here, you can leave the endIndex empty if you want to include elements till last.
  • step denotes the number of elements that we skip in myList before including the next element to newList

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.

Output:

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.

Output:

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.

Related Posts

  • Python check for null
    17 November

    How to check for null in Python

    Table of ContentsHow to check if a variable is NULL in Python?Using the is operator to check if a variable is NULL in Python.Using the if condition to check if a variable is NULL in Python.Using the not equal to operator to check if a variable is NULL in Python. This article demonstrates the different […]

  • Sort dictionary by value in Python
    17 November

    Sort dictionary by value in python

    Table of ContentsHow to sort sort dictionary by value in Python?Using for loop along with the sorted() function.Using the sorted() function to sort dictionary by value in Python.Using operator.itemgetter() function to sort sort dictionary by value in Python.Using lambda function to sort sort dictionary by value in Python.Using the OrderedDict() method to sort sort dictionary […]

  • Get class name in Python
    17 November

    Get class name in Python

    Table of ContentsHow to get the attributes of an object?Get Class name in python using the __class__ attribute Get Class name in python using type() functionConclusion While working with objects in python, several times we need to get their class name. In this article, we will discuss several ways to get the class name of an […]

  • Python print to file
    17 November

    How to Print to File in Python?

    Table of ContentsThe print() function in pythonHow to Print to file in python?Print to file using the file parameter in the print functionPrint to file by redirecting standard outputConclusion We use the print() function in python to print any text to standard output. In this article, we will see how we can use the print() […]

  • Python absolute path
    17 November

    Get Absolute Path in Python

    Table of ContentsWhat is an absolute path to a file? How to get absolute path in PythonUse abspath to get the absolute path to file use pathlib module to get the absolute path to file Conclusion We often use files to store the outputs of our programs in permanent storage. Generally, we work with relative […]

  • Python sort list of lists
    17 November

    Sort List of Lists in Python

    Table of ContentsSort List of Lists Using the sort() Method in PythonUsing the itemgetter() methodUsing Lambda FunctionsSort List of Lists Using the sorted() Function in PythonSort lists of lists on the basis of the length of the inner listsConclusion Several times we use 2-D lists or lists of lists in our programs to represent a […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.