国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2019-11-25 17:04:13
字體:
來源:轉載
供稿:網友

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所有邏輯運算符的操作:

#!/usr/bin/pythona = 10b = 20c = 0if ( 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 = 0if ( 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 trueLine 2 - Either a is true or b is true or both are trueLine 3 - Either a is not true or b is not trueLine 4 - Either a is true or b is true or both are trueLine 5 - Either a is not true or b is not true or both are not true

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 普安县| 扬中市| 曲松县| 沁阳市| 梓潼县| 德江县| 苏尼特左旗| 华宁县| 宝兴县| 富锦市| 巴彦县| 余姚市| 吉安市| 安图县| 堆龙德庆县| 石景山区| 巍山| 东乡族自治县| 通海县| 介休市| 偃师市| 肃南| 洪洞县| 湟源县| 汉川市| 从化市| 永仁县| 新绛县| 台中县| 达拉特旗| 阿拉善右旗| 墨玉县| 八宿县| 青冈县| 泾源县| 保靖县| 海南省| 宁南县| 镇巴县| 莎车县| 开鲁县|