Python Comment

Python supports styles of comments:

  • Single-line comments
  • Multi-line comments

Single-Line Comments:

Single-line comments start with the # symbol and increase until the give up of the road. Anything after # on that line is considered a comment.

                                  
                                    # This is a single-line comment
x = 5  # You can also add a comment at the end of a line                          
                                  
                                

Multi-Line Comments:

Triple quotes (""" or ''') may be used for multi-line comments. While Python would not have a selected syntax for multi-line remarks, the use of triple costs for strings at the beginning of a block of code efficiently serves the motive of a multi-line remark.

                                  
                                    """
This is a
multi-line
comment
"""                           
                                  
                                

Commenting Out Code:

You also can use remarks to temporarily disable or "remark out" a bit of code with out disposing of it totally. This is useful for checking out alternative code or debugging.

                                  
                                    # x = 10  # Commenting out code to temporarily disable it                            
                                  
                                

Comments for Explanation:

Comments may be used to give an explanation for the reason or logic in the back of a selected line or block of code. This makes the code extra readable and comprehensible for others (or your self) who would possibly evaluation the code later.

                                  
                                    # Calculate the sum of two numbers
num1 = 5
num2 = 10
result = num1 + num2       
                                  
                                

Remember that even as feedback are helpful, it is important to preserve clean and readable code. Avoid over-commenting or including remarks that simply repeat the code. Focus on supplying insights that aren't immediately apparent from the code itself.