Nurdaulet Kenges Nurdaulet Kenges - 4 months ago 8
Java Question

How to convert expression "<<=1" in Java to Python

In the code below:

for (int N = 2; N <= 128; N <<= 1) {print(N)}


It will print: 2,4,8,16,32,64,128

How can I create such loop in Python? So how I could express "<<=1" in Python?

Answer

This for-loop has to be converted into a while-loop:

n = 2
while n <= 128:
    print n
    n <<= 1