Minon Weerasinghe Minon Weerasinghe - 5 months ago 26
Python Question

For loop iterate over powers of 2

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


How could I do this?

Answer
counter = 2

while counter <= 1024:
    print counter
    counter *= 2