
# 空值:
# python中一种特殊的值,用None表示,空值的类型也是一种特殊数据类型
# None不能理解为0,因为0是有意义的,而None是一个特殊值。
# 空值通常用作默认值使用
# 创建一个空值
a = None
print(a)
print(type(a))

# 布尔:
# 用来存储一个真或假的数值,只有真 True或者假 False两种值
# True 和 False 是 Python 中的关键字,当作为 Python 代码输入时,一定要注意字母的大小写,否则解释器会报错
# 布尔值可以当作一个整数来对待,即 True 相当于整数值 1,False 相当于整数值 0
# 下方运算是可以执行的,只用于示例,实际运用中是不可以这样去用的
print(False + 1)
print(True + 1)

# 创建一个布尔值
boo = True
print(boo)
print(type(boo))
boo1 = False
print(boo1)
print(type(boo1))

# 布尔值的使用
# 布尔值一般存在于if判断语句、while循环、for循环中,用于判断条件真或假
# 布尔值使用场景:流程控制,逻辑分支
# 两个数值在比较时会产生布尔值
# a == b
# a>=b
# ……
# 布尔数据类型转换
# 布尔转换为其它数据类型
# 布尔转换为int
# True转换为int是:1,False转换为int是:0
b = True
c = False
b1 = int(b)
print(b1)
print(type(b1))
c1 = int(c)
print(c1)
print(type(c1))
# 布尔转换为str
c2 = str(c)
print (c2)
print(type(c2))

# 其它类型转换为布尔
# 空值转换为布尔
d = None
print(bool(d))
# int转换为布尔
# 数字转换布尔,只有0转换为False,其余数字都是True
f = -100
print(bool(f))
f1 = 0
print(bool(f1))

# str转换为布尔,任意有数据的字符串转换为布尔值都为True
c3 = "Hello"
e5 = bool(c3)
print (e5)
print(type(e5))
r = 123
e6 = bool(r)
print(e6)
print(type(e6))
# 只有字符串为空字符串时转换为布尔值为False,双引号中什么数据都没有
t = ""
e7 = bool(t)
print(e7)
print(type(e7))

# list转换为布尔:空列表转换为False,有数据的列表转换为True
g = []
print(bool(g))
g1 = ["12","23"]
print(bool(g1))
# tuple转换为布尔
h = (1,2,3)
print(bool(h))
h1 = ()
print(bool(h1))
# dict转换为布尔
j = {"we":"123","fd":234}
print(bool(j))
j1 = {}
print(bool(j1))
# set转换为布尔
k = ("123")
print(bool(k))
k1 = set()
print(bool(k1))

# list,tuple,dict,set中如果存在数据,转换为布尔都是True,空的就是False
# 特殊值False和None、各种类型(整数,浮点数等)的数值0、空序列(空列表,空元组,空字符串)、空字典都视为假,其他各种值都为真,包括特殊值True.
# 逻辑运算符,布尔值的运算
# and 逻辑与
# or 逻辑或
# not 逻辑非
x = True
x1 = False
x2 = not x
print(x)
print(x2)
x3 = not x1
print(x1)
print(x3)
x4 = x and x1
print(x4)
x5 = x or x1
print(x5)

# and 与 所有条件为真,结果为真,有一个为假,结果为假
# or 或 所有条件假,结果为假,有一个为真,结果为真
# not 非 相反
(文章转载:微观世间百态)