1. 操作符
- 24.8.28 10:17 created
1.1. and
a = True
b = False
print(a and b) # False
1.2. or
a = True
b = False
print(a or b) # True
1.3. not
a = True
print(not a) # False
1.4. operator - &
a = 60 # 0000 1100
b = 13 # 0000 1101
print(a & b) # 12 0000 1100
1.5. operator - |
a = 60 # 0000 1100
b = 13 # 0000 1101
print(a | b) # 61 0000 1101
1.6. operator - ^
a = 60 # 0000 1100
b = 13 # 0000 1101
print(a ^ b) # 49 0011 0001
1.7. operator - ~
a = 60 # 0000 1100
print(~a) # -61
1.8. operator - <<
a = 60 # 0000 1100
print(a << 2) # 240 1111 0000
1.9. operator - >>
a = 60 # 0000 1100
print(a >> 2) # 15 0000 1111
1.10. Truthy and Falsy Values
以下Collections、Sequences是假的(Falsy):
- Empty lists []
- Empty tuples []
- Empty dictionaries {}
- Empty sets set()
- Empty strings ""
- Empty ranges range(0)
以下numbers是假的:
- Integer: 0
- Float: 0.0
- Complex: 0j
以下constant是假的:
- None
- False
真的值:
- 不为空的Collections、Sequences
- 不为0的数字
- True
# case 1
print(3 and 1) # True
# case 2
if 3:
print(True)
if []:
print("empty list is true in boolean context")
else:
print("empty list is false in boolean context") # 走这里
print(bool("")) # False
print(bool("Wilson")) # True
1.11. short circuit
- 例如:如果
a and b
,a为False
,那么不需要检查b,这个表达式就是False
- 例如:如果
a or b
, a为True
,那么也不需要检查b,这个表达式就是True
# case 1
print([] and []) # []
# case 2
if 2 or (10 / 0):
print("we got no error")