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

首頁 > 編程 > Python > 正文

Python中的字典與成員運算符初步探究

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

Python元字典
字典(dictionary)是除列表以外python之中最靈活的內置數據結構類型。列表是有序的對象結合,字典是無序的對象集合。
兩者之間的區別在于:字典當中的元素是通過鍵來存取的,而不是通過偏移存取。
字典用"{ }"標識。字典由索引(key)和它對應的值value組成。

#!/usr/bin/python# -*- coding: UTF-8 -*-dict = {}dict['one'] = "This is one"dict[2] = "This is two"tinydict = {'name': 'john','code':6734, 'dept': 'sales'}print dict['one'] # 輸出鍵為'one' 的值print dict[2] # 輸出鍵為 2 的值print tinydict # 輸出完整的字典print tinydict.keys() # 輸出所有鍵print tinydict.values() # 輸出所有值

輸出結果為:

This is one This is two {'dept': 'sales', 'code': 6734, 'name': 'john'} ['dept', 'code', 'name'] ['sales', 6734, 'john']

Python成員運算符
除了以上的一些運算符之外,Python還支持成員運算符,測試實例中包含了一系列的成員,包括字符串,列表或元組。

以下實例演示了Python所有成員運算符的操作:

#!/usr/bin/pythona = 10b = 20list = [1, 2, 3, 4, 5 ];if ( a in list ):  print "Line 1 - a is available in the given list"else:  print "Line 1 - a is not available in the given list"if ( b not in list ):  print "Line 2 - b is not available in the given list"else:  print "Line 2 - b is available in the given list"a = 2if ( a in list ):  print "Line 3 - a is available in the given list"else:  print "Line 3 - a is not available in the given list"

以上實例輸出結果:

Line 1 - a is not available in the given listLine 2 - b is not available in the given listLine 3 - a is available in the given list

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新绛县| 泸水县| 珠海市| 通榆县| 西平县| 元谋县| 凌源市| 汶川县| 乌兰县| 宁德市| 来宾市| 洪湖市| 阳山县| 维西| 碌曲县| 苗栗县| 广宗县| 高雄市| 商洛市| 巫溪县| 巴彦县| 汾阳市| 安塞县| 长乐市| 息烽县| 福海县| 克拉玛依市| 开化县| 郁南县| 宁南县| 府谷县| 会东县| 闵行区| 巴东县| 南召县| 晋中市| 宜章县| 岑巩县| 石屏县| 马尔康县| 安吉县|