首页 > 文章列表 > python字典如何进行运算

python字典如何进行运算

Python字典
487 2022-08-07

说明

1、字典运算中的键必须是不可变类型,如整数(int)、浮点数(float)、字符串(str)、元组(tuple)等。

2、列表(list)和集合(set)不能作为字典中的键,当然字典本身也不能作为字典中的键。

因为字典也是可变类型,但字典可以作为字典中的值。

实例

student1 = {
    'id': 1010,
    'name': '小明',
    'sex': 'True',
    'birthday': '2000-1-1'
}
#遍历字典中的键
for key in student1:            """
    print(key, student1[key])
#遍历字典中的值
for value in student1.values():
    print(value)
#遍历字典中的键值对
for key, value in student1.items():
    print(key, value)

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。