For loop with variable python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Prerequisite: Python For loops

    The for loop has a loop variable that controls the iteration. Not all the loops utilize the loop variable inside the process carried out in the loop.  

    Example:

    Python3

    print("Using the loop variable inside :")

    for i in range(0, 5):

        x = (i+1)*2

        print(x, end=" ")

    print("\nUsing the loop variable only for iteration :")

    for j in range(0, 5):

        print('*', end=" ")

    Output

    Using the loop variable inside :
    2 4 6 8 10 
    Using the loop variable only for iteration :
    * * * * * 

    In the code snippet above, in loop-1,the loop control variable ‘i‘ is used inside the loop for computation. But in loop-2, the loop control variable ‘j‘ is concerned only with keeping tracking of the iteration number. Thus, ‘j’ is an unused variable in for loop. It is good practice to avoid declaring variables that are of no use.  Some IDEs like Pycharm, PyDev, VSCode produce warning messages for such unused variables in the looping structure. The warning may look like something given below:

    For loop with variable python

    To avoid such warnings, the convention of naming the unused variable with an underscore(‘_’) alone can be used. This avoids the problem of unused variables in for loops. Consider the following script with an unused loop variable tested with the Vulture module in Python. Once the vulture module is installed using the pip command, it can be used for testing .py scripts in the anaconda prompt. 

    Example: trial1.py

    Python3

    def my_func():

        a = 5

        b = 2

        c = b+2

        print(b, c)

    for i in range(0, 5):

        print("*", end=" ")

    Checking with vulture module

    To avoid this unused variable ‘i’ warning, the loop variable can simply be replaced by an underscore (‘_’). Look at the code snippet below

    Python3

    def my_func():

        b = 2

        c = b+2

        print(b, c)

    for _ in range(0, 5):

        print("*", end=" ")

    Some use pylint, a tool to keep track of code styles and dead code in Python. Such a tool can raise a warning when the variable in for loop is not used. To suppress that, it is better to use the underscore naming conventions for the unused variables.


    Can you put variable in for loop Python?

    A Python for loop has two components: A container, sequence, or generator that contains or yields the elements to be looped over. In general, any object that supports Python's iterator protocol can be used in a for loop. A variable that holds each element from the container/sequence/generator.

    Can you put a for loop in a variable?

    When you declare a variable inside a for loop, there is one important point to remember: the scope of that variable ends when the for statement does. (That is, the scope of the variable is limited to the for loop.) ...

    What is the variable in a for loop Python?

    The loop variable <var> takes on the value of the next element in <iterable> each time through the loop. In this example, <iterable> is the list a , and <var> is the variable i . Each time through the loop, i takes on a successive item in a , so print() displays the values 'foo' , 'bar' , and 'baz' , respectively.

    How do you do a for loop with two variables?

    For Loop with two variables in C++.
    #include <iostream>.
    // for loop with two variable i & j..
    // i will start with 0 and keep on incrementing till 10..
    // j will start with 10 and keep on decrementing till 0..
    for (int i = 0, j = 10; i < 10 && j > 0; i++, j--).
    std::cout << "i = " << i << " :: " << "j = " << j << std::endl;.