日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

詳解Python中的元組與邏輯運算符

系統 1746 0

Python元組
元組是另一個數據類型,類似于List(列表)。
元組用"()"標識。內部元素用逗號隔開。但是元素不能二次賦值,相當于只讀列表。

            
#!/usr/bin/python
# -*- coding: UTF-8 -*-

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')

print tuple # 輸出完整元組
print tuple[0] # 輸出元組的第一個元素
print tuple[1:3] # 輸出第二個至第三個的元素 
print tuple[2:] # 輸出從第三個開始至列表末尾的所有元素
print tinytuple * 2 # 輸出元組兩次
print tuple + tinytuple # 打印組合的元組


          

以上實例輸出結果:

            
('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')

          

以下是元組無效的,因為元組是不允許更新的。而列表是允許更新的:

            
#!/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 # 元組中是非法應用
list[2] = 1000 # 列表中是合法應用


          

Python邏輯運算符
Python語言支持邏輯運算符,以下假設變量a為10,變量b為20:
詳解Python中的元組與邏輯運算符_第1張圖片
以下實例演示了Python所有邏輯運算符的操作:

            
#!/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"


          

以上實例輸出結果:

            
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
          


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!??!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 平乡县| 镇原县| 浪卡子县| 敖汉旗| 酒泉市| 孝义市| 瑞安市| 汶上县| 肇东市| 上高县| 安庆市| 泰和县| 彩票| 永宁县| 马山县| 白山市| 江油市| 桑植县| 南昌市| 竹北市| 博罗县| 巨鹿县| 英超| 桑植县| 遂平县| 黄陵县| 游戏| 芒康县| 调兵山市| 莱芜市| 婺源县| 浙江省| 灌云县| 高州市| 洞口县| 遂溪县| 朝阳区| 昆明市| 黄梅县| 德化县| 承德市|