在Python语言中,
任何非零的数值、
非空的数据类型都等价于True,
0或空类型等价于False,
可以直接用作判断条件。
————————《Python语言程序设计二级教程》Page69
在if语句中将列表名用在条件表达式中时,
Python将在列表至少包含一个元素时返回True,
并在列表为空时返回False。
————————《Python编程从入门到实践》Page78
各种类型中的某些值会自动被转换为False,其他的值则是True。
如:None、整数中的0、浮点数中的0.0、
字符串中的空串''、空列表[]、空元组()、
空字典{}、和空集set()都会被当做False。
————————《Python语言基础与应用》链接-->
https://www.icourse163.org/learn/PKU-1003479006?tid=1450218470#/learn/content?type=detail&id=1214420434
https://www.icourse163.org/learn/PKU-1003479006?tid=1450218470#/learn/content?type=detail&id=1214420434&cid=1218119202
我们可以使用bool()方法将这些数据类型转为布尔类型。
代码演示:
info = \
"""
在Python语言中,
任何非零的数值、
非空的数据类型都等价于True,
0或空类型等价于False,
可以直接用作判断条件。
————————《Python语言程序设计二级教程》Page69
在if语句中将列表名用在条件表达式中时,
Python将在列表至少包含一个元素时返回True,
并在列表为空时返回False。
————————《Python编程从入门到实践》Page78
各种类型中的某些值会自动被转换为False,其他的值则是True。
如:None、整数中的0、浮点数中的0.0、
字符串中的空串''、空列表[]、空元组()、
空字典{}、和空集set()都会被当做False。
————————《Python语言基础与应用》链接-->
https://www.icourse163.org/learn/PKU-1003479006?tid=1450218470#/learn/content?type=detail&id=1214420434
https://www.icourse163.org/learn/PKU-1003479006?tid=1450218470#/learn/content?type=detail&id=1214420434&cid=1218119202
我们可以使用bool()方法将这些数据类型转为布尔类型。
"""
# print(info)
print("----"*10)
# 测试None
condition = None
print("condition的类型是:",type(condition))
print(condition,"#打印",condition,"的布尔值:",bool(condition))
if condition:
print(condition,"进入判断分支******************** True")
else:
print(condition,"进入判断分支$$$$$$$$$$$$$$$$$$$$ False")
print(condition,"测试结束\n\n\n")
# ----------------------------------------
# condition的类型是: <class 'NoneType'>
# None #打印 None 的布尔值: False
# None 进入判断分支$$$$$$$$$$$$$$$$$$$$ False
# None 测试结束
# 测试整数0
condition = 0
print("condition的类型是:",type(condition))
print(condition,"#打印",condition,"的布尔值:",bool(condition))
if condition:
print(condition,"进入判断分支******************** True")
else:
print(condition,"进入判断分支$$$$$$$$$$$$$$$$$$$$ False")
print(condition,"测试结束\n\n\n")
# condition的类型是: <class 'int'>
# 0 #打印 0 的布尔值: False
# 0 进入判断分支$$$$$$$$$$$$$$$$$$$$ False
# 0 测试结束
# 测试浮点数0.0
condition = 0.0
print("condition的类型是:",type(condition))
print(condition,"#打印",condition,"的布尔值:",bool(condition))
if condition:
print(condition,"进入判断分支******************** True")
else:
print(condition,"进入判断分支$$$$$$$$$$$$$$$$$$$$ False")
print(condition,"测试结束\n\n\n")
# condition的类型是: <class 'float'>
# 0.0 #打印 0.0 的布尔值: False
# 0.0 进入判断分支$$$$$$$$$$$$$$$$$$$$ False
# 0.0 测试结束
# 测试空字符串''
condition = ''
print("condition的类型是:",type(condition))
print(condition,"#打印",condition,"的布尔值:",bool(condition))
if condition:
print(condition,"进入判断分支******************** True")
else:
print(condition,"进入判断分支$$$$$$$$$$$$$$$$$$$$ False")
print(condition,"测试结束\n\n\n")
# condition的类型是: <class 'str'>
# #打印 的布尔值: False
# 进入判断分支$$$$$$$$$$$$$$$$$$$$ False
# 测试结束
# 测试空列表
# condition = list()
# condition = [1,2,3]
condition = []
print("condition的类型是:",type(condition))
print(condition,"#打印",condition,"的布尔值:",bool(condition))
if condition:
print(condition,"进入判断分支******************** True")
else:
print(condition,"进入判断分支$$$$$$$$$$$$$$$$$$$$ False")
print(condition,"测试结束\n\n\n")
# condition的类型是: <class 'list'>
# [] #打印 [] 的布尔值: False
# [] 进入判断分支$$$$$$$$$$$$$$$$$$$$ False
# [] 测试结束
# 测试空元组
# condition = (2,3,4)
condition = ()
# condition = tuple()
print("condition的类型是:",type(condition))
print(condition,"#打印",condition,"的布尔值:",bool(condition))
if condition:
print(condition,"进入判断分支******************** True")
else:
print(condition,"进入判断分支$$$$$$$$$$$$$$$$$$$$ False")
print(condition,"测试结束\n\n\n")
# condition的类型是: <class 'tuple'>
# () #打印 () 的布尔值: False
# () 进入判断分支$$$$$$$$$$$$$$$$$$$$ False
# () 测试结束
# 测试空字典
condition = {}
# condition = {"key1","value1"}
# condition = dict()
print("condition的类型是:",type(condition))
print(condition,"#打印",condition,"的布尔值:",bool(condition))
if condition:
print(condition,"进入判断分支******************** True")
else:
print(condition,"进入判断分支$$$$$$$$$$$$$$$$$$$$ False")
print(condition,"测试结束\n\n\n")
# condition的类型是: <class 'dict'>
# {} #打印 {} 的布尔值: False
# {} 进入判断分支$$$$$$$$$$$$$$$$$$$$ False
# {} 测试结束
# 测试空集合
condition = set()
# condition = {1,2,3}
print("condition的类型是:",type(condition))
print(condition,"#打印",condition,"的布尔值:",bool(condition))
if condition:
print(condition,"进入判断分支******************** True")
else:
print(condition,"进入判断分支$$$$$$$$$$$$$$$$$$$$ False")
print(condition,"测试结束\n\n\n")
# condition的类型是: <class 'set'>
# set() #打印 set() 的布尔值: False
# set() 进入判断分支$$$$$$$$$$$$$$$$$$$$ False
# set() 测试结束