Python CommentsWe'll study how to write comments in our program in this article. We'll also learn about single-line comments, multi-line comments, documentation strings, and other Python comments. Introduction to Python CommentsWe may wish to describe the code we develop. We might wish to take notes of why a section of script functions, for instance. We leverage the remarks to accomplish this. Formulas, procedures, and sophisticated business logic are typically explained with comments. The Python interpreter overlooks the remarks and solely interprets the script when running a program. Single-line comments, multi-line comments, and documentation strings are the 3 types of comments in Python. Advantages of Using CommentsOur code is more comprehensible when we use comments in it. It assists us in recalling why specific sections of code were created by making the program more understandable. Aside from that, we can leverage comments to overlook specific code while evaluating other code sections. This simple technique stops some lines from running or creates a fast pseudo-code for the program. Below are some of the most common uses for comments:
Types of Comments in PythonIn Python, there are 3 types of comments. They are described below: Single-Line CommentsSingle-line remarks in Python have shown to be effective for providing quick descriptions for parameters, function definitions, and expressions. A single-line comment of Python is the one that has a hashtag # at the beginning of it and continues until the finish of the line. If the comment continues to the next line, add a hashtag to the subsequent line and resume the conversation. Consider the accompanying code snippet, which shows how to use a single line comment: Code Output: This statement does not have a hashtag before it The following is the comment: The Python compiler ignores this line. Everything following the # is omitted. As a result, we may put the program mentioned above in one line as follows: Code Output: This is not a comment This program's output will be identical to the example above. The computer overlooks all content following #. Multi-Line CommentsPython does not provide the facility for multi-line comments. However, there are indeed many ways to create multi-line comments. With Multiple Hashtags (#) In Python, we may use hashtags (#) multiple times to construct multiple lines of comments. Every line with a (#) before it will be regarded as a single-line comment. Code In this case, each line is considered a comment, and they are all omitted. Using String Literals Because Python overlooks string expressions that aren't allocated to a variable, we can utilize them as comments. Code We can observe that on running this code, there will be no output; thus, we utilize the strings inside triple quotes(""") as multi-line comments. Python DocstringThe strings enclosed in triple quotes that come immediately after the defined function are called Python docstring. It's designed to link documentation developed for Python modules, methods, classes, and functions together. It's placed just beneath the function, module, or class to explain what they perform. The docstring is then readily accessible in Python using the __doc__ attribute. Code Output: This function adds the values of x and y Next TopicPython If Else |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India