Comments are pieces of information present in the middle of code that allows a developer to explain his work to other developers. They make the code more readable and hence easier to debug.
An inline comment is a single line comment and is on the same line as a statement. They are created by putting a ‘#’ symbol before the text.
# This is a single line commment
Note: Although not necessary, according to Python there should be a single space between # symbol and comment text and at least 2 spaces between comment and the statement.
Block comments in Python usually refer to the code following them and are intended to the same level as that code. Each line of block comment starts with a ‘#’ symbol.
# This is a block comment # Each line of a block comment is intended to the same level
GFG GFG GFG GFG GFG
The documentation string is string literal that occurs as the first statement in a module, function, class, or method definition. They explain what can be achieved by the piece of code but should not contain information about the logic behind the code. Docstrings become __doc__ special attribute of that object which makes them accessible as a runtime object attribute. They can be written in two ways:
1. One-line docstring:
"""This is a one-line docstring."""
'''This is one-line docstring.'''
Hello Geek! Help on function my_fun in module __main__: my_fun() Greets the user.
Note that for one-line docstring closing quotes are on the same line as opening quotes.
2. Multi-line docstring:
"""This is a multi-line docstring. The first line of a multi-line doscstring consist of a summary. It is followed by one or more elaborate description. """
Hello Geek! Help on function my_fun in module __main__: my_fun(user) Greets the user Keyword arguments: user -- name of user
The closing quotes must be on a line by themselves whereas opening quotes can be on the same line as the summary line.
Some best practices for writing docstrings:
- Use triple double-quotes for the sake of consistency.
- No extra spaces before or after docstring.
- Unlike comments, it should end with a period.
- Comments in Python
- How to write Comments in Python3?
- Python Docstrings
- 10 Essential Python Tips And Tricks For Programmers
- Python if else
- Python | Method Overloading
- Difference between == and is operator in Python
- Python range() function
- Garbage Collection in Python
- Python Basics
- How to print without newline in Python?
- Code introspection in Python
- Python | hex() function
- Python program to print even length words in a string
- Python Variables
- Python Object Comparison : "is" vs "=="
- Internal working of Python
- Find length of a string in python (4 ways)
- Why Python is called Dynamically Typed?
- Multi-Line printing 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.