滑滑滑稽稽稽

滑滑滑稽稽稽

这里是·弔人の博客~ 俺の愛馬が!
bilibili
telegram
github

Beginner's Python Learning Notes · Dictionary Chapter

Learning knows no bounds, we will never stop

Basic format of a dictionary:
(d is the name of the dictionary)

d = {
        "key1": "value1", 
        'key2 (number)': 123
         }

I am a separator~


Ways to add dictionary values:

d["I am a new key"] = "I am a new value"

or:

d.setdefault("I am a new key","I am a new value")

Ways to delete key-value pairs:

del d["I am a new key"]
#Delete a single key-value pair

or:

d.popitem()
#Delete a random key-value pair

I am a separator~


dict.items()
↑Used when iterating over a dictionary, feels like magic

Example:
key = {
    "nima" : "Python",
    "niniang" : "Java" ,
    "ni" : "C#"
}
for key1,values in  key.items():
    print(key1,values)
Output:
nima Python
niniang Java
ni C#

I am a separator~


Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.