在Python中,字典可以通过将元素的序列卷曲内创建{}括号,由“逗号”分离。Dictionary包含一对值,一个是Key,另一个是对应的pair元素
Key:value
。字典中的值可以是任何数据类型,可以复制,而键不能重复,必须是不可变的。
字典也可以通过内置函数dict()创建。只需放置大括号{}就可以创建一个空字典。注 -字典键区分大小写,名称相同但Key的不同情况将被明确区分。
# Creating an empty Dictionary
Dict = {}
print("Empty Dictionary: ")
print(Dict)
# Creating a Dictionary
# with Integer Keys
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
# Creating a Dictionary
# with Mixed keys
Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
# Creating a Dictionary
# with dict() method
Dict = dict({1: 'Geeks', 2: 'For', 3:'Geeks'})
print("\nDictionary with the use of dict(): ")
print(Dict)
# Creating a Dictionary
# with each item as a Pair
Dict = dict([(1, 'Geeks'), (2, 'For')])
print("\nDictionary with each item as a pair: ")
print(Dict)
输出:








暂无数据