applying for loop such that counters are multiplied rather than being added in python -


hello relatively new python! there way using loops in python? java implementation of want in python

for (i=1;i<20; i*= 2) {system.out.println(i);} 

solution in while loop in python`

while i<20:     print     i*=2  

i cannot figure out way using loops. implemented using while loop obviously, still curious know whether there method or not

there lots of ways this, e.g.

for in range(5):     = 2 **     print 

or using generators

from itertools import count, takewhile  def powers_of_two():     in count():         yield 2 **  in takewhile(lambda x: x < 20, powers_of_two()):     print 

but in end, depends on use case version gives clearest , readbale code. in cases, use while-loop, since it's simple , job.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -