首頁  >  文章  >  後端開發  >  Boolean運算和真假值

Boolean運算和真假值

高洛峰
高洛峰原創
2016-10-19 15:03:301816瀏覽

在python中,任何物件都可以判斷其真假值:True,False

在if或while條件判斷中,下面的情況值為False:


1.None 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.

many types are always true.


在運算操作和內建函數回傳Boolean結果0或Flase表示false


1或True表示true

運行結果:

>>>

x or y -> if x is false,then y, else x

or 0 = 2

or 10 = 10

or 0 = 2

or 10 = 10

or 0 = 2

or 10 = 10

##############################################

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

>>>

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn