资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

python中Bool运算和真假值的示例

这篇文章将为大家详细讲解有关python中Bool运算和真假值的示例,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

成都创新互联公司-专业网站定制、快速模板网站建设、高性价比义乌网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式义乌网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖义乌地区。费用合理售后完善,十载实体公司更值得信赖。

在python中,任何对象都可以判断其真假值:True,False

在if或while条件判断中,下面的情况值为False:

1.None

2.Flase

3.数值为0的情况,如:0,0.0,0j

4.所有空序列,如:'',(),[]

5.所有空mapping,如:{}

6.instances of user-defined classes, if the class defines a __bool__() or __len__() method,

   when that method returns the integer zero or bool value False.

All other values are considered true — so objects of many types are always true.

在运算操作和内建函数返回Boolean结果0或者Flase表示false(更多学习内容,请点击创新互联)

1或True表示true

python中的Boolean运算如下:

print('x or y -> if x is false,then y, else x ')
x, y= 2,0
print('{} or {} = {}'.format(x, y, xor y))
x1, y1= 0,10
print('{} or {} = {}'.format(x1, y1, x1or y1))
x2, y2= 0,0
print('{} or {} = {}'.format(x2, y2, x2or y2))
  
print('#' * 50)
print('x and y -> if x is false,then x, else y ')
print('{} and {} = {}'.format(x, y, xand y))
x1, y1= 0,10
print('{} and {} = {}'.format(x1, y1, x1and y1))
x2, y2= 0,0
print('{} and {} = {}'.format(x2, y2, x2and y2))
  
print('#' * 50)
print('not x -> if x is false,then True,else False ')
x= 2
print('not {} = {}'.format(x,not x))
x= 0
print('not {} = {}'.format(x,not x))

运行结果:

>>>
x or y -> if x is false,then y, else x
or 0 = 2
or 10 = 10
or 0 = 0
##################################################
x and y -> if x is false,then x, else y
and 0 = 0
and 10 = 0
and 0 = 0
##################################################
not x -> if x is false,then True,else False
not 2 = False
not 0 = True
>>>

关于python中Bool运算和真假值的示例就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网站栏目:python中Bool运算和真假值的示例
URL分享:http://cdkjz.cn/article/ijjcgc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220