python中的0,None,False,空容器

在Python中,None、空列表[]、空字典{}、空元组()、0等一系列代表空和无的对象会被转换成False。除此之外的其它对象都会被转化成True。

# 判断dict为空:
if not dict1: #不可用is None.
    print("字典为空")

1.0等于False,这点要注意.

2.空的list判断:

if 0 == len(list) 或者 if list  #空的list相当于False

而if len(list)则表示list不为空时,因为len(list)在等于0的时候为False,其他情况下为True.

3.判断是否为None:

if X is None

if not X  #因为None代表False

新战场:https://blog.csdn.net/Stephen___Qin
原文地址:https://www.cnblogs.com/Stephen-Qin/p/10276470.html