YouTube Icon

Code Playground.

How to Comment in Python

CFG

How to Comment in Python

When composing Python code, it is consistently a decent practice to make your code clean and effectively justifiable. Sorting out the code, giving factors and capacities clear names are a few different ways to do this. 

Another approach to improve the clarity of your code is to utilize remarks. A remark is an intelligible clarification or comment that is utilized to clarify the code. For instance, in the event that you composed a complex regex, you include a remark depicting what the code does. 

Adding remarks to your Python code will spare you a ton of time and exertion when you take a gander at your code later on. Suppose you need to change a content that you composed a couple of months or years prior. The odds are that you won't recollect why you thought of some confounded bit of code except if you included a remark. The remarks additionally help different designers to comprehend your code and its motivation. 

Remarks ought to be short and direct. Try not to clarify something that is evident to the peruser. 

This article covers the rudiments of composing remarks in Python. 

Writing Comments in Python

Python disregards everything composed on the line after the hash mark (#). 

Remarks can be included toward the starting the line or inline with other code: 


# This is a Python comment.
print("Hello World") # This is an inline Python comment.

The clear space after the hash mark isn't required, however it will improve the remark's intelligibility. 

A hash character inside a string exacting doesn't show the beginning of a remark line. It is just a hash character: 

paragraph = "# Hash inside quotes is not a comment."

Comments should be at the same indent level as the code beneath it:

```py
def factorial(n):
  if n == 0:
    return 1
  else:
    # Use the factorial function
    return n * factorial(n-1)

On the off chance that your content tool underpins punctuation featuring, remarks are typically spoken to in green. 

Remarks are additionally valuable while troubleshooting a content. Rather than erasing a few lines or squares, you can remark them out: 

# for fruit in fruits:
#   print(fruit)

Multiline Comments in Python (Comment Blocks)

In contrast to other famous programming dialects, Python underpins just single line remarks. 

The most straightforward approach to compose multiline remarks in Python is to include single line remarks in a steady progression: 


# This is the first line.
# This is the second line.

Another alternative is to utilize docstrings . 

Docstrings are multiline string literals that are utilized to archive what a module, capacity, class, or strategy does. 

A docstring starts and finishes with triple twofold statements (""") and can range on one or different lines: 

"""This is
a multiline
docstring.
"""

Docstrings are not actually remarks. At the point when a docstring happens as the principal explanation in a module, capacity, class, or strategy, it winds up in the bytecode and turns into the __doc__ exceptional quality of that object. You ought to favor utilizing normal single-line hash remarks. 

Shebang

In case you're perusing Python contents, you may see that on some of them the primary line start with the #! characters and the way to the Python translator: 

#!/usr/bin/env python3

This succession of characters is called kit n kaboodle and is utilized to advise the working framework which translator to use to parse the remainder of the document. Contents that start with kit n kaboodle and are executable can be run in the terminal without composing python before the content name. 

Since the kit n kaboodle line begins with the hash character, it is considered as a remark and consequently overlooked by the Python translator. 

Conclusion 

Composing remarks is a decent practice and helps different engineers, including future self, to comprehend what the code does. In Python, everything after the hash mark (#) and until the stopping point is viewed as a remark. 

On the off chance that you have any inquiries or criticism, don't hesitate to leave a remark.




CFG