In this post you will learn about comments in python and how to write single-line and multi-line comments in python.
What are comments?
Comments are texts that are ignored by the python interpreter and never going to execute.They are written so that programmers can understand what a particular line or block of code is doing.In below sections,we will see how we can write comments in python.
Single line comment
Single line comments are preceded by a “#” symbol.You can write single line comments before starting of code,or you can write single line comments after the code on the same line,like shown below.
#This is a comments print("Single line comment") print("Hello") #Comment on same line
Multi-line comments in python
Python does not supports multi-line comments directly.But,we can use some hacks to add multi-line comments in python.Below I have mentioned 2 ways in which you can add multi-line comments in python.
1: Repeated Single Line comment
This is the real and recommended way of adding multi-line comments in python by PEP8 style guide. You write multiple single line comments before the code like below.
#This is a #multiline #comments print("Multiline comment")
Now,you must be thinking that,this is very time consuming and irritating because you have to write “#” in front of every line to make it a comment.One thing you can do is,you can use multi-line editing features of various editors like sublime text,atom,notepad++ etc,to edit multiple lines at the same time.
2: Multi-line strings
We can use multi-line string that are enclosed withing triple inverted commas ( “””—–“””) as a comments. Now, this is the hack which we are using to add a multi-line comments in our code. But,this is not a true comments. This is like,adding a string to our code without assigning it to a variable and never using that string in our code.
"""This is a way of adding multi-lne comment""" """This is a way of adding multi-lne comment """ """this multiline comment will give error"""
In above example,I have written 3 multi-line comments.First and Second one will not give any error.Third one will give a Indentation error,because starting “”” of the Third comment is indented,so that’s why we need to always use proper indentation for the starting “”” of the multi-line comments. For the ending “””, we don’t have any restriction on indentation,we can write it anywhere on the same line or on the next line of comments.
3: Multi-line string comment in functions and classes
We can use multi-line string comments in functions and classes too.But,one thing we need to remember is that,whenever multi-line string is written directly after the function signature or class signature,then it’s not treated as a comments,in that case it’s treated as a docstring.
def square(num): #Function signature """This will become docdring as it is after the function signature""" print(num*num) def cube(num): #Function signature print(num*num*num) """This will become a multiline string commnets as it is not after the function signature"""
Now,lets see what __doc__ prints for both of these functions.We can print docstring of these functions using print (function_name.__doc). After printing docstring for above two function,the output is
Docstring for Square: This will become docdring as it is after the function signature Docstring for Cube: None
As you can see from output,doctring for Cube function prints nothing because the multi-line comment was not after the function signature.But in square function,as multi-line comment was after the function signature,it turned into function docstring instead of multi-line comment.
So,that all for this tutorial.I hope,I explained everything about Python’s single-line and Multi-line comments in a way.if you have any doubt or any suggestion,feel free to comment down below.