Python中的邏輯運算子

來源:互聯網
上載者:User

標籤:not   用法   []   bsp   strong   span   family   detail   lang   

參考資料:http://blog.csdn.net/sasoritattoo/article/details/12451359

 

一、邏輯判斷詞not

1.在python中not是邏輯判斷詞,用於布爾型True和False,not True為False,not False為True,以下是幾個常用的not的用法:

(1) not與邏輯判斷句if連用,代表not後面的運算式為False的時候,執行冒號後面的語句。比如:

a = False

if not a:   (這裡因為a是False,所以not a就是True)

    print "hello"

這裡就能夠輸出結果hello

(2) 判斷元素是否在列表或者字典中,if a not in b,a是元素,b是列表或字典,這句話的意思是如果a不在列表b中,那麼就執行冒號後面的語句,比如:

a = 5

b = [1, 2, 3]

if a not in b:

    print "hello"

這裡也能夠輸出結果hello

2.在python中 None,  False, Null 字元串"", 0, 空列表[], 空字典{}, 空元組()都相當於False

例子1:

 

b= None
if not b:
print 1
else:
print 0
輸出結果為 1

 

例子2:

 

b= False
if not b:
print 1
else:
print 0
輸出結果為 1

 

例子3:

 

b= ‘‘
if not b:
print 1
else:
print 0
輸出結果為 1i

 

當把b的值改為0、空列表[]、空字典{}或空元組()時,輸出結果一樣的,在此就不一一列出

 



 




Python中的邏輯運算子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.