How to add elements to python dictionary

藏色散人
Release: 2019-11-29 10:29:33
Original
14406 people have browsed it
Recommended manual:Python basic introductory tutorial

How to add elements to python dictionary

python How to add elements to dictionary?

Dictionary is another mutable container model and can store any type of object.

Each key-value key=>value pair in the dictionary is separated by colon:. Each key-value pair is separated by comma,. The entire dictionary is included in curly braces {}. The format is as follows:

d = {key1 : value1, key2 : value2 }
Copy after login

The way to add new content to the dictionary is to add new key/value pairs

Example:

#!/usr/bin/python
 
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
 
dict['Age'] = 8 # 更新
dict['School'] = "RUNOOB" # 添加
 
 
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']
Copy after login

The output result of the above example:

dict['Age']:  8
dict['School']:  RUNOOB
Copy after login
Recommended related articles:
1.How to create a dictionary in python
2.How to add and delete key values ​​in a python dictionary
3.The difference between python lists and dictionaries
Related video recommendations:

1.Little Turtle Zero Basics Learning Python Video Tutorial

Related recommendations: "Python Tutorial"

The above is the detailed content of How to add elements to python dictionary. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!