2016-03-20 25 views
-4

在下面的代码:如何表达转换 “<< = 1” 的Java到Python

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

它会打印:2,4,8,16,32,64,128

哪有我在Python中创建这样的循环?那么我如何在Python中表达“< < = 1”呢?

+1

你可能只是乘 - 'N * = 2'?但是Python确实有一个[复合赋值移位运算符](http://zetcode.com/lang/python/operators/),所以你可以按原样使用这个代码... –

+1

'<< = 1'会在Python中是'<< = 1'。 – vaultah

回答

0

这for循环必须被转换成while循环:

n = 2 
while n <= 128: 
    print n 
    n <<= 1 
相关问题