What is equivalent to
for (I=2; I<n; I*=2)
in python. I tried to do this with range() function and I know how it works with increment only. But here I want to multiply it in each iteration. I don't want to do it manually in side the loop
While while is easy, while is also slow. Try:
In [3]: import math
In [4]: n = 10
In [5]: for i in (2**n for n in range(1, int(math.log2(n)) + 1)):
...: print(i)
...:
2
4
8
In [6]: n = 40
In [7]: for i in (2**n for n in range(1, int(math.log2(n)) + 1)):
...: print(i)
...:
2
4
8
16
32
In [8]: n = 60
In [9]: for i in (2**n for n in range(1, int(math.log2(n)) + 1)):
...: print(i)
...:
2
4
8
16
32
In [10]: n = 100
In [11]: for i in (2**n for n in range(1, int(math.log2(n)) + 1)):
...: print(i)
...:
2
4
8
16
32
64
Simple is the rule in python. While is easy:
n = 20
i = 2
while (i<n):
print "the i variable is "+str(i)
i = i * 2
I (originally, this has since been changed) used addition for this simple example, multiplication would simply use the "*" operator as you did.
0 * 2is0.