Comments are text notes added to the program to provide explanatory information about the source code. They are used in a programming language to document the program and remind programmers of what tricky things they just did with the code and also helps the later generation for understanding and maintenance of code. The compiler considers these as non-executable statements. Since comments do not execute, when you run a program you will not see any indication of the comment in the output.
The hash(#) symbol denotes the staring of a comment in Python.
# This is a comment in Python
Comments should be made at the same indent as the code it is commenting on.
Hello!! GeeksforGeeks Welcome to Comments in Python
Types of Comments :
1. Single-Line Comments: Comments starting with a ‘#’ and whitespace are called single-line comments in Python. These comments can only stretch to a single line and are the only way for comments in Python. e.g.
# This a single line comment.
2. Multi-line (Block) comments: Unlike other programming languages Python doesn’t support multi-line comment blocks out of the box. HOwever we can use consecutive # single-line comments to comment out multiple lines of code. Some examples of block comments-
# This type of comments can serve # both as a single-line as well # as multi-line (block) in Python.
3. Inline Style comments: Inline comments occur on the same line of a statement, following the code itself. Generally, inline comments look like this:
x = 3 # This is called an inline comment a = b + c # Adding value of 'b' and 'c' to 'a'
4. Docstring comments: Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods. It’s specified in source code that is used, like a comment, to document a specific segment of code. Unlike conventional source code comments, the docstring should describe what the function does, not how.
Using __doc__: Demonstrates docstrings and does nothing really. Using help: Help on function my_function in module __main__: my_function() Demonstrates docstrings and does nothing really.
Advantages and Uses of Comments :
- Planning and reviewing : In comments, we can write the pseudocode which we planned before writing the source code. Pseudocode is a mixture of natural language and high-level programming language. This helps in reviewing the source code more easily because pseudocode is more understandable than the program.
- Debugging : Brute force method is a common method of debugging. In this approach, print statements are inserted throughout the program to print the intermediate values with the hope that some of the printed values will help to identify the errors. After doing debugging we comment those print statements. Hence comment is also used for debugging.
- Python2 vs Python3 | Syntax and performance Comparison
- Convert String to Double in Python3
- Introduction to Python3
- Python3 - if , if..else, Nested if, if-elif statements
- Different Input and Output Techniques in Python3
- New '=' Operator in Python3.8 f-string
- Positional-only Parameter in Python3.8
- How to print spaces in Python3?
- How to use Variables in Python3?
- Comments in Python
- How do we create multiline comments in Python?
- How to take integer input in Python?
- Python Program to Check Whether a Number is Positive or Negative or zero
- How to Round Numbers in Python?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.