问Python如何给字典中添加元素?

答 1 个回答|203 次阅读
Coco老师 - 官方公众号:青少儿编程学习网
擅长:编程教育网站:https://kidscodes.cn/

方法一:直接添加,给定键值对

#pycharm
aa = {'人才':60,'英语':'english','adress':'here'}
print(aa) # {'人才': 60, '英语': 'english', 'adress': 'here'}
#添加方法一:根据键值对添加
aa['价格'] = 100
aa['价格'] = 100 # {'人才': 60, '英语': 'english', 'adress': 'here', '价格': 100}

方法二:使用update方法

#添加方法二:使用update方法添加
xx = {'hhh':'gogogo'}
aa.update(xx)
print(aa) # {'人才': 60, '英语': 'english', 'adress': 'here', '价格': 100, 'hhh': 'gogogo'}

推荐课程 »更多

    推荐知识

    Python精选库大全,青少年Python编程学习总结

    Python最适合青少儿进阶学习的编程语言