首页 > 编程知识 正文

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

时间:2023-11-25 09:12:53 阅读:308952 作者:BYCL

编写一个 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'}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。