Python for loop increment by multiplication

Would this code increment i by 1 or not?

    for i in range(100):
         print(i)
         i*=2

asked May 11, 2020 at 19:23

Python for loop increment by multiplication

5

It will increment by 1. When you use an iterator like range(), you assign the next value from the iterator each time, and whatever you assigned to i in the loop is ignored.

If you want it to double each time, don't use range(), do your own updating and end test.

i = 1
while (i < 100):
    print(i)
    i *= 2

answered May 11, 2020 at 19:35

BarmarBarmar

694k53 gold badges468 silver badges576 bronze badges

i will start from the first value of the sequence generated by iterator (range() in this case) and then it will get multiplied by 2 and it'll retain this value until the next iteration of loop at which it will get the next value of the sequence generated by iterator range() so,

 for i in range(5):
         print("Before update")
         print(i)
         i*=2
         print("After update")
         print(i)

Output:

Before Update
0
After Update
0
Before Update
1
After Update
2
Before Update
2
After Update
4
Before Update
3
After Update
6
Before Update
4
After Update
8
Before Update
5
After Update
10

answered May 11, 2020 at 19:33

user_3pijuser_3pij

1,2729 silver badges21 bronze badges

2

0 points

about 7 years

my_list = [0,1,2,3,6,5,7]

for i in range(0,len(my_list),2**i): print my_list[i]

this code gives an error

Answer 55e1c6b5d3292fba3e0000c2

The code is fine apart from the for loop line. Read that line again. for i in range(0, len(my_list), 2**i) means to take the next value from the range and then assign it to i. So first, Python has to generate the range. This means that i can only be assigned a value when the range has been generated. This is how things will go in your case:

  1. Python sees the for loop
  2. Python tries to generate a range (it doesn’t know what i is yet)
  3. It notices that you have passed i to the range() method, it doesn’t know what i is so it generates a NameError (which basically means that it doesn’t know what i is a name of)

This is how they should have gone:

  1. Python sees the for loop
  2. It tries to generate a range. Generation is successful.
  3. It assigns the first value of the range to i

As you can see, in your case, i has not been created yet and you are already trying to pass it in as an argument to the range() function. If you tell me what you are trying to do then I could fix your code.

points

Python for loop increment by multiplication

about 7 years

Answer 55e28f289113cb1cd400054d

points

Python for loop increment by multiplication

about 7 years

Answer 55e3b13b937676a569000576

Try the following:

import math l = list(range(0,100)) for i in range(0, int(math.log(len(l),2))+1): print l[2**i - 1]

Note that the last line is indented but does not show in the preview.

points

about 7 years

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    For loops, in general, are used for sequential traversal. It falls under the category of definite iteration. Definite iterations mean the number of repetitions is specified explicitly in advance. But have you ever wondered, what happens, if you try to increment the value of the iterator from inside the for loop. Let’s see with the help of the below example.
    Example:
     

    Python3

    lis = [1, 2, 3, 4, 5]

    for i in range(len(lis)):

        print(lis[i])

        i += 2

    Output:
     

    1
    2
    3
    4
    5

    The above example shows this odd behavior of the for loop because the for loop in Python is not a convention C style for loop, i.e., for (i=0; i 

    • Using While loop: We can’t directly increase/decrease the iteration value inside the body of the for loop, we can use while loop for this purpose.
      Example:
       

    Python

    lis = [1, 2, 3, 4, 5]

    i = 0

    while(i < len(lis)):

        print(lis[i], end = " ")

        i += 2

    • Output: 
       
    1 3 5
    •  
    • Using another variable: We can use another variable for the same purpose because after every iteration the value of loop variable is re-initialized.
      Example: 
       

    Python

    lis = [1, 2, 3, 4, 5]

    i = 0

    for j in range(len(lis)):

        if(i >= len(lis)):

            break

        print(lis[i], end = " ")   

        i += 2

    • Output: 
       
    1 3 5
    •  
    • Using Range Function: We can use the range function as the third parameter of this function specifies the step.
      Note: For more information, refer to Python range() Function.
      Example:
       

    Python3

    lis = [1, 2, 3, 4, 5]

    for i in range(0, len(lis), 2):

        print(lis[i], end = " ")   

    • Output:
       
    1 3 5
    •  

    How do you use a for loop in Python for multiplication?

    Method 1: Using For loop.
    number = int(input ("Enter the number of which the user wants to print the multiplication table: ")).
    # We are using "for loop" to iterate the multiplication 10 times..
    print ("The Multiplication Table of: ", number).
    for count in range(1, 11):.
    print (number, 'x', count, '=', number * count).

    How do I make an incrementing loop in Python?

    In python, if you want to increment a variable we can use “+=” or we can simply reassign it “x=x+1” to increment a variable value by 1. After writing the above code (python increment operators), Ones you will print “x” then the output will appear as a “ 21 ”. Here, the value of “x” is incremented by “1”.

    Can we use multiplication in for loop?

    Step 1: Enter a number to print table at runtime. Step 2: Read that number from keyboard. Step 3: Using for loop print number*I 10 times. // for(i=1; i<=10; i++) Step 4: Print num*I 10 times where i=0 to 10.

    Can you increment i in a for loop?

    A for loop doesn't increment anything. Your code used in the for statement does. It's entirely up to you how/if/where/when you want to modify i or any other variable for that matter.