qzmjef 发表于 2024-8-17 21:08:25

Python3中真真假假True、False、None等含义详解


    <div style="color: black; text-align: left; margin-bottom: 10px;">
      <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/34f95cf9ce874fdf847587a4a9978c94~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895716&amp;x-signature=5KXueYNsYHRLvvj1GU2QNPPs%2FXo%3D" style="width: 50%; margin-bottom: 20px;"></div>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">概述</strong></p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在Python中,<span style="color: black;">不仅</span>和类C<span style="color: black;">同样</span>的真假类似,<span style="color: black;">例如</span>1<span style="color: black;">表率</span>真,0<span style="color: black;">表率</span>假。Python中的真假有着更加广阔的含义范围,Python会把所有的空数据结构视为假,<span style="color: black;">例如</span>[](空列表)、{}(空集合)、(空字符串)等,而与之相反的非空数据结构即为真</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">简单对比代码:</strong></p># 遍历列表中的示例元素,获取对应的真假:
      for elenment in [, S, [], , {}, {3, SSS}, 0, 0.0, 1, None]:
      if elenment:
      print(elenment, True)
      else:
      print(elenment, False)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">示例结果:</strong></p>False
      S True
      [] False
       True
      {} False
      {SSS, 3} True
      0 False
      0.0 False
      1 True
      None False<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">None对象</strong></p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在Python中None<span style="color: black;">不仅</span><span style="color: black;">表率</span>False,它本身<span style="color: black;">便是</span>一个特殊的空对象,<span style="color: black;">能够</span>用来占位,<span style="color: black;">例如</span><span style="color: black;">咱们</span><span style="color: black;">能够</span>利用None实现类似C中定义数组的方式,预定义列表的<span style="color: black;">体积</span>,实现对可能的索引进行赋值,而为赋值的索引都为None</p>L = * 10
      print(L)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">空列表定义结果</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">布尔(bool)值</strong></p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在Python中布尔值,True和False<span style="color: black;">不仅</span><span style="color: black;">能够</span><span style="color: black;">暗示</span>真与假,<span style="color: black;">乃至</span><span style="color: black;">能够</span>用于数学运算:</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">python print(True+1) print(False+1) print(True+False) ¨G4G 2 1 1 ¨G5G python print(isinstance(True, int)) print(isinstance(False, int)) ¨G6G True True</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">即实质上在Python中布尔值本身是整型(int),即bool类型<span style="color: black;">便是</span>int类型的子类。</p>
    </div>




页: [1]
查看完整版本: Python3中真真假假True、False、None等含义详解