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.whjierui.cn/article/dhshddj.html

其他资讯