比较运算符示例
功能要求
关系运算符示例
实例代码
print(1 < 3 < 5) # 等价于1 < 3 and 3 < 5
print(3 < 5 > 2)
print(1 > 6 < 8)
print("Hello" > 'hello') # 比较字符串大小
print([1, 2, 3] < [1, 2, 4]) # 比较列表大小
print({1, 2, 3} < {1, 2, 3, 4}) # 测试是否为子集
print({1, 2, 3} == {3, 2, 1}) # 测试两个集合是否相等
print({1, 2, 4} > {1, 2, 3}) # 集合之间的包含测试
print({1, 2, 4} < {1, 2, 3})
print({1, 2, 4} == {1, 2, 3})
print('Hello' > 3) # 字符串和数字不能比较
运行结果
Traceback (most recent call last):
File "E:\Code\PythonCode\hello.py", line 11, in <module>
print('Hello' > 3) # 字符串和数字不能比较
TypeError: '>' not supported between instances of 'str' and 'int'
True
True
False