{"id":28267,"date":"2022-08-28T14:51:30","date_gmt":"2022-08-28T09:21:30","guid":{"rendered":"https:\/\/python-programs.com\/?p=28267"},"modified":"2022-08-28T14:56:18","modified_gmt":"2022-08-28T09:26:18","slug":"comments-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/comments-in-python\/","title":{"rendered":"Comments in Python"},"content":{"rendered":"
Comments are descriptions that help programmers better understand the program’s intent and functionality.<\/p>\n
The Python interpreter completely ignores them.<\/p>\n
Advantages of Comments\u00a0<\/strong><\/p>\n The use of comments in programs improves the readability of our code. It improves the readability of the program and helps us remember why certain blocks of code were written.<\/p>\n Aside from that, comments can be used to ignore certain code while testing other blocks of code. This provides a simple way to prevent the execution of certain lines or to quickly write pseudo-code for the program.<\/p>\n Python comments are classified into three types:<\/p>\n Single-line comments in Python have proven to be useful for providing concise descriptions of parameters, function definitions, and expressions.<\/p>\n To write a single-line comment in Python, use the hash symbol (#) <\/strong>with no white space and continues until the end of the line. If the comment continues to the next line, add a hashtag and resume the conversation.<\/p>\n Example1<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation<\/strong><\/p>\n Everything following #<\/strong> is ignored. As a result, we can write the above program in a single line as shown below:<\/p>\n Example2<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation<\/strong><\/p>\n Multiline comments are not supported in Python. However, there are other methods for writing multiline comments.<\/p>\n Writing multiline comments using Multiple Hashtags (#):<\/strong><\/p>\n In Python, we can use multiple hashtags (#) to write multiline comments. Every line will be treated as a single-line comment.<\/p>\n Output:<\/strong><\/p>\n Writing multiline comments using String Literals:<\/strong><\/p>\n Python ignores string literals that are not assigned to a variable, so we can use them as comments.<\/p>\n Example1<\/strong><\/p>\n Explanation<\/strong><\/p>\n Similarly, we can write multiline comments using multiline strings (triple quotes).<\/p>\n ( ‘ )or ( ” )<\/strong> symbols can be used as the quotation character.<\/p>\n Example2<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation<\/strong><\/p>\n Python docstrings are the strings enclosed in triple quotes that appear immediately after the defined function. It’s intended to link documentation created for Python modules, methods, classes, and functions. It is placed immediately beneath the function, module, or class to explain what they do. Using the __doc__ attribute, the docstring is then easily accessible in Python.<\/p>\n A documentation string is a string literal used as the first lines of a code block, such as a function.<\/p>\n A documentation string, unlike a regular comment, can be accessed at runtime via the obj.__doc__<\/strong> attribute, where obj is the name of the function.<\/p>\n Typically, a documentation string is used to generate code documentation automatically.<\/p>\n Docstrings are documentation strings.<\/p>\n Docstrings are not, strictly speaking, comments. They define anonymous variables that contain references to the strings. They are also not ignored by the Python interpreter.<\/p>\n Python has two types of docstrings:<\/p>\n One-line docstrings:<\/strong><\/p>\n A one-line docstring, as the name implies, fits one line. A one-line docstring starts with triple quotes (“””) and ends with triple quotes (“””). There will also be no blank lines before or after the one-line docstring.<\/p>\n Example<\/strong><\/p>\n Output:<\/strong><\/p>\n Multi-line docstrings:<\/strong><\/p>\n A multi-line docstring, as compared to a one-line docstring, can span multiple lines. A multi-line docstring also begins with triple quotes (“””) and ends with triple quotes (“””).<\/p>\n Example<\/strong><\/p>\n How Can We Write More Effective Comments?<\/strong><\/p>\n Comments are descriptions that help programmers better understand the program’s intent and functionality. The Python interpreter completely ignores them. Python code can be explained using comments. Comments can be used to improve the readability of the code. When testing code, comments can be used to prevent execution. Advantages of Comments\u00a0 The use of comments in …<\/p>\n\n
Single line Comments<\/strong><\/h4>\n
# Print the string\r\nprint(\"Good Morning Everyone !!! This is Admin, Hello this is PythonPrograms\")<\/pre>\n
Good Morning Everyone !!! This is Admin, Hello this is PythonPrograms<\/pre>\n
Here the \"Print the string\" is the comment. \r\nThe Python interpreter ignores this line.<\/pre>\n
print(\"Good Morning Everyone !!! This is Admin, Hello this is PythonPrograms\") # Print the string<\/pre>\n
Good Morning Everyone !!! This is Admin, Hello this is PythonPrograms<\/pre>\n
Here it will produce the same results as Example1. \r\nAll the text following (#) is ignored by the interpreter.<\/pre>\n
Multiline Comments<\/strong><\/h4>\n
\n
# This is an example for writting multiline comments\r\n# in python using multiple hashtags(#)\r\nprint(\"Good Morning Everyone !!! This is Admin, Hello this is PythonPrograms\")<\/pre>\n
Good Morning Everyone !!! This is Admin, Hello this is PythonPrograms<\/pre>\n
# Good Morning Everyone !!! This is Admin, Hello this is PythonPrograms\r\n'Writing multiline comments using String Literals'<\/pre>\n
The second line of the code is a string, but it is not assigned to any\r\nvariable or function. As a result, the interpreter ignores the string.<\/pre>\n
'''\r\n\r\nThis is an example for writting multiline comments\r\nin python using string literals\r\n\r\n'''\r\nprint(\"Good Morning Everyone !!! This is Admin, Hello this is PythonPrograms\")<\/pre>\n
Good Morning Everyone !!! This is Admin, Hello this is PythonPrograms<\/pre>\n
The interpreter ignores the multiline string because it is not \r\nassigned to any variable. It is technically not a multiline comment, \r\nbut it can be used as one.<\/pre>\n
Python Docstring<\/strong><\/h4>\n
\n
def add(x, y):\r\n \"\"\"Here it adds the x, y values and print the result\"\"\"\r\n return x+y\r\n\r\n\r\n# Print the docstring of the above declared add() function \r\nprint(add.__doc__)\r\n<\/pre>\n
Here it adds the x, y values and print the result<\/pre>\n
def add(x, y):\r\n \"\"\" Here we are writing the \r\n multiline docstrings\r\n by writing the function that accepts 2 variables\r\n as arguments \r\n \"\"\"<\/pre>\n
\n