国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Python學(xué)習(xí)(四)數(shù)據(jù)結(jié)構(gòu)——bool

2019-11-14 17:19:58
字體:
供稿:網(wǎng)友

Python 布爾類型 bool

  python 中布爾值使用常量True 和 False來表示;注意大小寫

  比較運(yùn)算符< > == 等返回的類型就是bool類型;布爾類型通常在 if 和 while 語句中應(yīng)用

  這邊需要注意的是,python中,bool是int的子類(繼承int),故 True==1  False==0 是會(huì)返回Ture的,有點(diǎn)坑,如要切實(shí)判斷用 xxx is True

1 PRint(True==1)                        # 返回True2 print(False==0)                       # 返回True3 print(1 is True)                    4 print(0 is False)

    另外,還有幾個(gè)坑。。。 如,Python2中True/False不是關(guān)鍵字,因此我們可以對(duì)其進(jìn)行任意的賦值;同理,Python 中 if(True) 的效率遠(yuǎn)比不上 if(1)

 True = "True is not keyWord in Python2"        # Python2 版本中True False不是關(guān)鍵字,可被賦值,Python3中會(huì)報(bào)錯(cuò) 

    另,由于bool是int,可進(jìn)行數(shù)字計(jì)算  print(True+True) 

 

True or False 判定

  以下會(huì)被判定為 False :

  • None
  • False
  • zero of any numeric type, for example, 0, 0.0, 0j.
  • any empty sequence, for example, '', (), [].
  • any empty mapping, for example, {}.
  • 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.

  除了以上的,其他的表達(dá)式均會(huì)被判定為 True,這個(gè)需要注意,與其他的語言有比較大的不同。

 1 print(bool()) 2 print(bool(False)) 3 print(bool(0),bool(0.0),bool(0j)) 4 print(bool(""),bool(()),bool([]),bool({})) 5 class alfalse(): 6     def __bool__(self):           # 定義了 __bool__() 方法,始終返回False 7         return False 8 f = alfalse() 9 print(bool(f))10 class alzero():11     def __len__(self):            # 定義了 __len__() 方法,始終返回012         return 013 zero = alzero()14 print(bool(zero))15 class justaclass():16     pass17 c = justaclass()18 print(bool(c))                    # 一般class instance都返回為True

 

布爾運(yùn)算符 and   or   not

 

OperationResult
x or yif x is false, then y, else x
x and yif x is false, then x, else y
not xif x is false, then True, else False

 

    注意均為小寫: and or not  ;  注意布爾運(yùn)算的優(yōu)先級(jí)低于表達(dá)式, not a == b 相當(dāng)于 not (a == b), 若 a == not b 就會(huì)有語法錯(cuò)誤

 1 print((True and False),(False and False),(True and True)) 2 print((True or False),(False or False),(True or True)) 3 print((not True),(not False)) 4 print( 1>1 and 1<1 )                 # 表達(dá)式優(yōu)于bool運(yùn)算  相當(dāng)于 print( (1>1) and (1<1) ) 5 print( (1>1 and 1)<1)                # 加括號(hào)了,值就不一樣了 6 print(True and 1)                    # True and 數(shù)字,不建議這么使用,返回的是數(shù)字 7 print(True and 111) 8 print(False and 2)                   # False and 數(shù)字,返回False 9 print(not 1==1)10 T = True11 F = False12 # print(T == not F)                  # 會(huì)報(bào)錯(cuò)13 print(T == (not F))                  # 需加括號(hào)

 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁阳县| 深泽县| 清原| 庆云县| 玛沁县| 麻栗坡县| 镇巴县| 许昌市| 南和县| 泰安市| 夏津县| 成都市| 石狮市| 隆德县| 讷河市| 乐至县| 景洪市| 韩城市| 康马县| 静乐县| 乌苏市| 白玉县| 静安区| 丹寨县| 惠州市| 合江县| 依安县| 巨野县| 南投县| 崇阳县| 景洪市| 绥德县| 阿勒泰市| 安福县| 黔东| 师宗县| 盐城市| 喀喇| 花垣县| 宿州市| 额济纳旗|