For 循环遍历 2 的幂

2022-01-24 00:00:00 python loops range increment

问题描述

我想编写一个 for 循环,它会为每个循环迭代 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

我该怎么做?


解决方案

counter = 2

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

相关文章