美图齐众专注资阳网站设计 资阳网站制作 资阳网站建设
资阳网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Python程序:向字典添加键值对

创新互联python教程:

编写一个 Python 程序,通过一个实际例子将键值对添加到字典中。

向字典添加键值对的 Python 程序示例 1

在这个 python 程序中,我们使用字典更新功能将键值插入到字典中。

# Python Program to Add Key-Value Pair to a Dictionary

key = input("Please enter the Key : ")
value = input("Please enter the Value : ")

myDict = {}

# Add Key-Value Pair to a Dictionary in Python
myDict.update({key:value})
print("\nUpdated Dictionary = ", myDict)

将键值对插入字典的 Python 程序示例 2

这个 Python 程序是将键值插入字典的另一种方法。

# Python Program to Add Key-Value Pair to a Dictionary

key = input("Please enter the Key : ")
value = input("Please enter the Value : ")

myDict = {}

# Add Key-Value Pair to a Dictionary in Python
myDict[key] = value
print("\nUpdated Dictionary = ", myDict)

向字典输出添加键值对

Please enter the Key : M
Please enter the Value : Mango

Updated Dictionary =  {'M': 'Mango'}

分享名称:Python程序:向字典添加键值对
网站路径:http://www.zsjierui.cn/article/dhshddj.html

其他资讯