How to Comment Inside a Python Dictionary

Comments in python are very handy in increasing the readability and maintainability of code. Generally we use comments to describe functions and class descriptions, for documentation purposes or to explain why a statement has been written in the source code but there may be a situation that we need to explain why we have included certain data in a dictionary or a list. In this article, we will see the basic functioning of python dictionaries and will try to understand how we can add comment inside a python dictionary.

Working of a python dictionary

In Python, dictionaries are data structures that are used to store data in the form of key and value pairs.A python dictionary is defined using curly braces and key and value pairs are inserted in the dictionary separated by colon ":" while initializing or may be added after initialization using assignment statement.

A key-value pair in a python dictionary is called an item.

The simplest way to initialize a dictionary is as follows:

website_details={"name":"Pyhton For Beginners",
         "domain":"pythonforbeginners.com"
        }
print("dictionary is:")
print(website_details)
print("Keys in the dictionary are:")
print(website_details.keys())
print("values in the dictionary are:")
print(website_details.values())
print("itmes in the dictionay are:")
print(website_details.items())

Output:

dictionary is:
{'name': 'Pyhton For Beginners', 'domain': 'pythonforbeginners.com'}
Keys in the dictionary are:
dict_keys(['name', 'domain'])
values in the dictionary are:
dict_values(['Pyhton For Beginners', 'pythonforbeginners.com'])
itmes in the dictionay are:
dict_items([('name', 'Pyhton For Beginners'), ('domain', 'pythonforbeginners.com')])

In the above example, we can see that a python dictionary has been defined using curly braces and key-value pairs are specified in the dictionary which are separated by colon “:”.

The keys of the dictionary can be obtained using dict_name.keys(). Similarly, the values in the dictionary can be obtained using dict_name.values() and all the items(key-value pairs) can be obtained by using dict_name.items() method.

If we want to insert new key value pairs into the dictionary, we can do it as follows.

website_details={"name":"Pyhton For Beginners",
         "domain":"pythonforbeginners.com"
        }
#add new item to list
website_details["acronym"]="PFB"
print("dictionary is:")
print(website_details)
print("Keys in the dictionary are:")
print(website_details.keys())
print("values in the dictionary are:")
print(website_details.values())
print("itmes in the dictionay are:")
print(website_details.items())

Output:

dictionary is:
{'name': 'Pyhton For Beginners', 'domain': 'pythonforbeginners.com', 'acronym': 'PFB'}
Keys in the dictionary are:
dict_keys(['name', 'domain', 'acronym'])
values in the dictionary are:
dict_values(['Pyhton For Beginners', 'pythonforbeginners.com', 'PFB'])
itmes in the dictionay are:
dict_items([('name', 'Pyhton For Beginners'), ('domain', 'pythonforbeginners.com'), ('acronym', 'PFB')])

In the above example, we have added an item having key as “acronym” and value as “PFB“. We can access the values associated with keys of the dictionary as dict_name[key_name] as shown in the following code snippet.

website_details={"name":"Pyhton For Beginners",
         "domain":"pythonforbeginners.com"
        }
#add new item to list
website_details["acronym"]="PFB"
print(website_details["domain"])

Output:

pythonforbeginners.com

Working of single line comments in python

A single line python comment can be written by initializing the comment text with a # symbol. Single line comments terminate when a line break is encountered in the source code.

We can put single line comments by starting it on a newline or we can put a single line comment by putting # sign after a statement in the source code but it should be remembered that when a newline or line break is found in the source code, the comment terminates. It can be visualized in the following source code.

website_details={"name":"Pyhton For Beginners",
         "domain":"pythonforbeginners.com"
        }
#This is single line comment from start of line
website_details["acronym"]="PFB"#this is a single line comment after an statement
print(website_details["domain"])

Working of multi line comments in python

Theoretically speaking, Multi line comments do not exist in python. But we can implement  multi line comments using single line comment and triple quoted strings in python.

We can implement multi line comments using single line comment by inserting a # sign whenever a line break is encountered. In this way, the multi line comments are just depicted as a sequence of single line comments.

website_details={"name":"Pyhton For Beginners",
         "domain":"pythonforbeginners.com"
        }
#This is a multi line comment
#implemented using # sign
website_details["acronym"]="PFB"
print(website_details["domain"])

We can also use strings as multi line comments if we do not assign them to any variable.When the string isn’t assigned to any variable, they are parsed and evaluated by the interpreter but no byte code is generated because no address can be assigned to the strings. This affects the string working just as a comment. In this method, multi line comments can be declared using triple quotes.This can be seen as follows.

website_details={"name":"Pyhton For Beginners",
         "domain":"pythonforbeginners.com"
        }
"""This is a multiline comment
implemented with the help of 
triple quoted strings"""
website_details["acronym"]="PFB"
print(website_details["domain"])

Adding single line comment inside a python dictionary

We can add single line comments inside a python dictionary using # symbol as we do normally in other places. We just need to move the content after the comment to a new line so that the content of the dictionary doesn’t get commented out.

website_details={"name":"Pyhton For Beginners",
                 #This is a single line comment inserted inside a dictionary 
         "domain":"pythonforbeginners.com"
        }

website_details["acronym"]="PFB"
print(website_details["domain"])

Adding Multi line comment inside a python dictionary

We can add multi line comments inside python dictionary by only using # symbol. Theoretically, we can only add single line comments inside a python dictionary but we can use consecutive single line comments to simulate multi line comments.

website_details={"name":"Pyhton For Beginners",
                 #This is a multiline comment inside a dictionary
                 #inserted with the help of consecutive single line comments
         "domain":"pythonforbeginners.com"
        }

website_details["acronym"]="PFB"
print(website_details["domain"])

Using triple quoted string as comment inside a python dictionary won’t work and python interpreter will throw error.

website_details={"name":"Pyhton For Beginners",
                 """This is a multiline comment inside a dictionary
                 inserted with the help of triple quoted strings and
                 it will cause error"""
         "domain":"pythonforbeginners.com"
        }

website_details["acronym"]="PFB"
print(website_details["domain"])

Conclusion

In this article, we have seen the working of python dictionary, single line comments and multi line comments in python and then we tried to implement ways to insert single line comments and multi line comment inside a python dictionary. Stay tuned for more informative articles.

Leave a Reply

Your email address will not be published. Required fields are marked *