python1:布爾類型,python1布爾類型

來源:互聯網
上載者:User

python1:布爾類型,python1布爾類型

布爾類型使用者在條件或迴圈中作為條件判斷,這裡介紹python中的布爾類型。

True和False

python布爾類型對應兩個布爾值:True和False,分別對應1和0。看下面的例子
>>> True == 1
True
>>> False == 0
True
>>> True + False + 21
22

布林運算式的值

下面的值在作為布林運算式時,會被解譯器看作False:
    1、None;
    2、False;
    3、任何為0的數字類型,如:0,0.0,0j;
    4、任何空序列,如:'',(),[];
    5、任何空字典,例如:{};
    6、使用者定義的類執行個體,如果類定義了__bool__()或者__len__()方法,並且該方法返回0或者布爾值False。
其它所有值被解譯器看作True。
可以用bool來轉換其它值為布爾值:
>>> bool('')
False
>>> bool('this is a test')
True
>>> bool(42)
True
>>> bool(0)
False

布爾操作

下面是python中布爾操作:
    1、x or y:if x is false,then y, else x
    2、x and y:if x is false, then x, else y
    3、not x:if x is false, then True, else False

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.