python中字典的使用
作者:互联网
alien_0 = {'color': 'green', 'points': 5}
print(alien_0['color'])-------------->访问字典
print(alien_0['points'])
字典类型用大括号,里面是{‘’key‘:’value’}
添加键值
alien_0['x_position'] = 0
1、实际应用中
(1)先创建一个空字典,然后往里面放key:value
alien_0 = {}
alien_0['color'] = 'green'
alien_0['points'] = 5
(2)删除
del alien_0['points']
2、遍历字典
(1)遍历字典所有内容
user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}
for key, value in user_0.items():------------------------------->user.item()
print("\nKey: " + key)
print("Value: " + value)
(2) 遍历键值
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
for name in favorite_languages.keys():
prinh(‘name’)
(3)遍历值
for language in favorite_languages.values():
print(language.title())
标签:python,value,alien,points,key,使用,print,字典 来源: https://www.cnblogs.com/dddzy/p/15714835.html