Publish Forum topic Publish your post to the cyberwarzone forum for free and no registration!

Create a Python dictionary and add key value pairs

In this short python tutorial we will take a look at how to create a dictionary in Python. Dictionaries are pretty fast, and they do not allow duplicate keys, so that can be handy if used right.

In this tutorial we will be creating the ‘cyberweapons’ dictionary, and we will be adding the threat level values and the descriptions of those threat levels to the dictionary.

The code to create a dictionary

cyberweapons = {}

Now we are going to add the keys ‘threatlevel1’, ‘threatlevel2’ and ‘threatlevel3’ to the dictionary, and each one of the keys will have its own description value:

cyberweapons[“threatlevel1”] = “This is threatlevel one. Immediate action needed.”
cyberweapons[“threatlevel2”] = “This is threatlevel 2.”
cyberweapons[“threatlevel3”] = “This is threatlevel 3.”

Now if you want to print one specific key value, you can do this by doing the following:

print(cyberweapons[“threatlevel1”])

Please spread the word :)

  • RSS
  • Follow by Email
  • Facebook
  • Google+
    http://cyberwarzone.com/create-python-dictionary-add-key-value-pairs/">