Python comment symbol
Python programmers frequently use the comment system because, without it, things may quickly become very perplexing. The developers' helpful information is provided in the comments, which helps the reader understand the source code. The logic, or a portion of it, employed in the code is explained. In most cases, comments are beneficial to someone who will maintaining or improving your code after you are gone and unable to provide support. These are frequently mentioned as a helpful programming practices that do not affect the program's output but enhance readability across the board. In Python, comments refer to lines of code that the interpreter skips over while the programme is running. The use of comments makes the code easier to read and aids in thorough code comprehension by programmers.
Comment symbol: The following categories of comments with various notations or symbols are available in Python as different forms of code comments.
- Single-line comments
- Multi-line comments
- Docstring comments
Let us talk about each of the several types of symbols that are used in the above type comments.
1) Single-line comments
Python single-line comments begin with the hashtag symbol (#) and continue until the end of the line. If the comment spans more than one line, add a hashtag to the following line before continuing. Short explanations of variables, function declarations, and expressions can be provided using Python's single-line comments.
Let us look at a programme that illustrates how to utilise the comment symbol for single-line comments:
# print “Hallowman” for understanding print(“Hallowman”)
The hashtag (#) symbol is a comment that is used in single-line comments in Python programming.
2) Multi-Line Comments
Python does not offer multiline comments as an option. But there are other methods we can use to create multiline comments. It can be achieved by two ways.
- Using Multiple Hashtags (#) symbols
- Using String Literals symbols
Using Multiple Hashtags (#) symbols: Python allows us to write comments with several lines using multiple hashtags (#). The comment will be treated as a single line for every line.
An example of a multiline comment containing numerous hashtags (#) symbols.
# demo program to show # Multiline comments print(“it is a multiline comment”)
Using String Literals: We can use string literals as comments because Python ignores those that are not allocated to a variable.
Let us understand with ease using string laterals:
Example 1: The words in between these symbols are ignored
We can therefore use the following as a single-line comment:
Here, #it can be a comment
In both ways, these symbols are used as multiline comments. The second line of the programme is a string, as seen above, but it is not allocated to a variable or function. Thus, the string is ignored by the interpreter. Similarly, multi-line comments can be written using multi-line strings (triple quotes). It is possible to use either 'or' as the quotation character.
Since we know see there won't be any output after running the code above, we use the triple-quoted strings (""") as multiline comments.
""" a simple Python program to show multiline comments""" print(“it is a multiline comments”)
The interpreter disregards the multi-line string because it isn't set to a variable in this case. It can be used as a multi-line comment even though it isn't officially one.
3) Docstring comments
The string literals enclosed in triple quotes that follow the function in Python are known as docstrings. It is used to link the written documentation to the classes, methods, functions, and modules of Python. It is included immediately after the functions, modules, or classes to explain what they perform. Following that, Python's __doc__ property makes the docstring accessible. as in the above method, or function definition, "triple single quotes" or "triple double quotes" are used to declare the docstrings. There should be a docstring for each function. The help function or the object's __doc__ method can be used to obtain the docstrings.
Here triple quote symbol is used for the python code comment.
Let us have a demonstration program to know briefly.
def add(s, h): """Adds the value of s and h""" return s+h # Print the add function's documentation string. print(add.__doc__)
def my_function(): '''Demo of the triple double quotes docstrings and does nothing really.''' return None print("consuming __doc__:") print(my_function.__doc__) print("consuming code:") help(my_function)