For loop iterate over powers of 2

前端 未结 5 897
醉话见心
醉话见心 2021-01-28 16:55

I want to write a for loop that will iterate over the powers of 2 for each loop.

For example I would want a range like this:

2, 4, 8, 16, ... , 1024
         


        
5条回答
  •  灰色年华
    2021-01-28 17:35

    You say you want to iterate over the powers of 2 for each loop,

    Seeing your example, the formulation could be:

    Make a loop that multiply the initial by 2 untill it reach 1024.

    ii = 2
    while ii <= 1024: 
        print(ii)
        ii = ii*2
    

提交回复
热议问题