Py学习  »  Python

语法“exp1<<variable<<exp2”在Python中是如何工作的?

Fajela Tajkiya • 3 年前 • 1304 次点击  

当我在看书的时候,我发现了这个例子。

def apply_discount(product, discount):
    price = int(product['price'] * (1.0 - discount))
    assert 0 <= price <= product['price']
    return price

我从没见过语法 0 <= price <= product['price'] 之前,很明显,这是在测试价格,应该 >= 0 <= product['price'] .我测试了这个功能,它按预期工作。我想对语法做更多的测试 0<=价格<=产品[价格] .

a = 7
if 0 << a << 10:
    print('a is greater than or equal to 0 and less than or equal to 10')
else:
    print('a is not greater than or equal to 0 and less than or equal to 10')

它总是打印出来 a is not greater than or equal to 0 and less than or equal to 10 .为什么会这样?到底是什么 0 << a << 10 工作

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130393
 
1304 次点击  
文章 [ 3 ]  |  最新文章 3 年前