python小技巧

mac2022-06-30  28

1,换值

a, b = b, a

  

2,真值测试

if a and b:

真:任意非空字符串,任意非0数字,任意非空容器,其他任意非False

假:空的字符串,数字0,空的容器 [] () {} set(),None

备注:含有空格的字符串为真:

print(bool(' ')) # True print(bool('')) # False

  

3,分母为0的异常处理

def divide(a,b)      try:           return a/b      except ZeroDivisionError:           return None

  

 

转载于:https://www.cnblogs.com/guxh/p/10273576.html

最新回复(0)