Python Programming Comments

What is comment?

  • Comment is the special line in scripts which will be ignored by python interpreter.
  • Comments are very much helpful in the program to describe the logic and improves code readability.
  • Comment is also used to ignore unnecessary python scripts during execution.

How to do comment in python program?

  • Hash sign (#) is used to write comments in the code.

  • All characters after the # and till end of the line are part of the comment.

  • Interpreter ignores the comments.

Different types of comments in python?

  • One line comment
  • Multiline comments
  • Documentation strings

How to do single line comment in python program?

#!/usr/bin/python
# prints the string
print "First Python Code!";
Output:
$comment1.py
First Python Code!
here single line comment 'prints the string' is ignored by python interpreter in python scripts file.

How to do multiline comments in python program?

Multiple lines can be commented using ''' ... ''' or """ ... """
using 3 single quotes or 3 double quotes before and after python scripts lines.
#!/usr/bin/python                                                               
                                                                                
'''                                                                             
print "Hello All!"                                                              
print "Welcome to python"                                                       
'''                                                                             
input_str = raw_input("Please enter a string:")                                 
                                                                                
print("Entered string is %s" % input_str)   
Output:
$ python first.py
Please enter a string:hi
Entered string is hi
two print lines are ignored by python interpreter.
3 double quotes can also be used to comment multiple lines.
#!/usr/bin/python 
#!/usr/bin/python                                                               
                                                                                
"""                                                                             
print "Hello All!"                                                              
print "Welcome to python"                                                       
"""                                                                             
input_str = raw_input("Please enter a string:")                                 
                                                                                
print("Entered string is %s" % input_str)                                       
Output:
$ python first.py
Please enter a string:hi
Entered string is hi
We cannot use 3 single quotes in starting and 3 double quotes in end of the lines or vise versa.
#!/usr/bin/python                                                               
                                                                                
'''                                                                             
print "Hello All!"                                                              
print "Welcome to python"                                                       
"""                                                                             
input_str = raw_input("Please enter a string:")                                 
                                                                                
print("Entered string is %s" % input_str)  
Python interpreter error:
$ python first.py
  File "first.py", line 11
    
    ^
SyntaxError: EOF while scanning triple-quoted string literal

Documentation Strings

3 double quotes before and after python scripts lines are used to create documentation strings or docstrings.
First line should be short, and summary of object.
Second line should be empty.
Following lines should be one or more paragraphs describing the object.
#!/usr/bin/python                                                               
                                                                                
def sum(a, b):                                                                  
    """Sum method                                                               
                                                                                
    Calculates sum value of variable a and b,                                   
    returns sum value.                                                          
    """                                                                         
    return a+b                                                                  
                                                                                
result = sum(4,6)                                                               
                                                                                
print sum.__doc__                                                               
print("sum of 4 and 6 is %d" % result)  
Output:
$ python docstrings.py 
Sum method

    Calculates sum value of variable a and b,
    returns sum value.
    
sum of 4 and 6 is 10

Privacy Policy  |  Copyright@2017 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Free online programming tutorials

Email Facebook Google LinkedIn Twitter
^