您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)python中Bool運(yùn)算和真假值的示例,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
在python中,任何對象都可以判斷其真假值:True,F(xiàn)alse
在if或while條件判斷中,下面的情況值為False:
1.None
2.Flase
3.數(shù)值為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.
在運(yùn)算操作和內(nèi)建函數(shù)返回Boolean結(jié)果0或者Flase表示false(更多學(xué)習(xí)內(nèi)容,請點(diǎn)擊億速云)
1或True表示true
python中的Boolean運(yùn)算如下:
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))
運(yùn)行結(jié)果:
>>> 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 >>>
關(guān)于python中Bool運(yùn)算和真假值的示例就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。