Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How is the Python dictionary defined?

2025-04-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to define Python dictionary". In daily operation, I believe many people have doubts about how to define Python dictionary. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to define Python dictionary". Next, please follow the editor to study!

I. definition

A dictionary is a series of key-value pairs separated by colons, while key-value pairs are separated by commas, in which "key" and "value" are the left side of the cat, and "value" is the content to the right of the colon.

The key must be unique, but the value is not necessary.

For example, players in the game have nicknames, health values, attack power, mana and other attributes:

Nickname: player

Blood volume: 100%

Attack power: 100

Mana: 50

Then the Python dictionary can be written as follows, where the "value" of the reference "key" is in the format of: dictionary name [key]

Hero = {"nickname": "player", "Health value": "100%", "attack Power": "100", "Mana value": "50"} print (hero ["nickname"]) print (hero ["Health value]) print (hero [" attack "]) print (hero [" Mana "])

II. Operation

Create an empty dictionary name = {}.

Add key-value pair dictionary name ["key name"] = key value.

Modify key value dictionary name ["key name"] = new key value.

Delete key-value pair del dictionary name [key name] key-value pair cannot be recovered once deleted.

Clear (), empty the dictionary.

Dict (), rebuild the dictionary.

Days = {} days ["Monday"] = "12-20" days ["Tuesday"] = "12-21" days ["Wednesday"] = "12-22" days ["Thursday"] = "12-23" days ["Friday"] = "12-24" days ["Saturday"] = "12-25" days ["Sunday"] = "12-26" print (days) days ["Monday"] = "12-27" Print (days) del days [Monday "] print (days) days.clear () print (days)

Third, traversing the dictionary

For variable name 1, variable name 2 in dictionary name.items ()

Keys (), take the key name

Valuse (), with a value

The specific uses are as follows:

Hero = {"nickname": "player", "Health value": "100%", "attack Power": "100", "Mana value": "50"} for key,value in hero.items (): print (key+ ":" + value) for key in hero.keys (): print (key) for value in hero.values (): print (value)

At this point, the study of "how to define the Python Dictionary" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report