詳解Python中的元組與邏輯運(yùn)算符
Python元組
元組是另一個(gè)數(shù)據(jù)類型,類似于List(列表)。
元組用"()"標(biāo)識(shí)。內(nèi)部元素用逗號(hào)隔開。但是元素不能二次賦值,相當(dāng)于只讀列表。
#!/usr/bin/python # -*- coding: UTF-8 -*- tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) tinytuple = (123, 'john') print tuple # 輸出完整元組 print tuple[0] # 輸出元組的第一個(gè)元素 print tuple[1:3] # 輸出第二個(gè)至第三個(gè)的元素 print tuple[2:] # 輸出從第三個(gè)開始至列表末尾的所有元素 print tinytuple * 2 # 輸出元組兩次 print tuple + tinytuple # 打印組合的元組
以上實(shí)例輸出結(jié)果:
('abcd', 786, 2.23, 'john', 70.2) abcd (786, 2.23) (2.23, 'john', 70.2) (123, 'john', 123, 'john') ('abcd', 786, 2.23, 'john', 70.2, 123, 'john')
以下是元組無效的,因?yàn)樵M是不允許更新的。而列表是允許更新的:
#!/usr/bin/python # -*- coding: UTF-8 -*- tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tuple[2] = 1000 # 元組中是非法應(yīng)用 list[2] = 1000 # 列表中是合法應(yīng)用
Python邏輯運(yùn)算符
Python語言支持邏輯運(yùn)算符,以下假設(shè)變量a為10,變量b為20:
以下實(shí)例演示了Python所有邏輯運(yùn)算符的操作:
#!/usr/bin/python a = 10 b = 20 c = 0 if ( a and b ): print "Line 1 - a and b are true" else: print "Line 1 - Either a is not true or b is not true" if ( a or b ): print "Line 2 - Either a is true or b is true or both are true" else: print "Line 2 - Neither a is true nor b is true" a = 0 if ( a and b ): print "Line 3 - a and b are true" else: print "Line 3 - Either a is not true or b is not true" if ( a or b ): print "Line 4 - Either a is true or b is true or both are true" else: print "Line 4 - Neither a is true nor b is true" if not( a and b ): print "Line 5 - Either a is not true or b is not true or both are not true" else: print "Line 5 - a and b are true"
以上實(shí)例輸出結(jié)果:
Line 1 - a and b are true Line 2 - Either a is true or b is true or both are true Line 3 - Either a is not true or b is not true Line 4 - Either a is true or b is true or both are true Line 5 - Either a is not true or b is not true or both are not true
相關(guān)文章
wxPython中l(wèi)istbox用法實(shí)例詳解
這篇文章主要介紹了wxPython中l(wèi)istbox用法,以實(shí)例形式較為詳細(xì)的分析了Python使用wxPython中l(wèi)istbox的相關(guān)技巧,需要的朋友可以參考下2015-06-06動(dòng)感網(wǎng)頁相冊(cè) python編寫簡單文件夾內(nèi)圖片瀏覽工具
這篇文章主要為大家詳細(xì)介紹了動(dòng)感網(wǎng)頁相冊(cè)的制作方法,即利用python編寫簡單文件夾內(nèi)圖片瀏覽工具,感興趣的小伙伴們可以參考一下2016-08-08使用Django啟動(dòng)命令行及執(zhí)行腳本的方法
今天小編就為大家分享一篇使用Django啟動(dòng)命令行及執(zhí)行腳本的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05Pytorch之8層神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)Cifar-10圖像分類驗(yàn)證集準(zhǔn)確率94.71%
這篇文章主要介紹了Pytorch之8層神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)Cifar-10圖像分類驗(yàn)證集準(zhǔn)確率94.71%問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03python標(biāo)準(zhǔn)日志模塊logging的使用方法
python的標(biāo)準(zhǔn)庫里的日志系統(tǒng)從Python2.3開始支持。只要import logging這個(gè)模塊即可使用。2013-11-11Python中對(duì)象的比較操作==和is區(qū)別詳析
這篇文章主要給大家介紹了關(guān)于Python中對(duì)象的比較操作==和is區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02django之從html頁面表單獲取輸入的數(shù)據(jù)實(shí)例
這篇文章主要介紹了django之從html頁面表單獲取輸入的數(shù)據(jù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03python讀文件保存到字典,修改字典并寫入新文件的實(shí)例
下面小編就為大家分享一篇python讀文件保存到字典,修改字典并寫入新文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04