Get the App
SLTechnology News&Howtos  ›  Development  › 

How is the Python dictionary defined?

Shulou Source: shulou.com Published: 2022-06-01 16:04:55 10月02日 Update

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!

Tags: Dictionaries weeks attacks nicknames mana values attacks players learning between content colons variables more help practical next right attributes articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Microsoft Apple Shulou Technology Shulou Tech Info