This article is participating in Python Theme Month. See the link to the event for more details
directory
- ๐ช how to use single-line comments in Python?
- ๐ฐ how to use multi-line comments in Python?
- ๐ฌ 2.1 Use hash tags for comments
- ๐ฏ 3. How to use docstrings in Python?
- ๐ซ 3.1 Use triple quotes for docstrings
- ๐ก 3.2 What is the difference between a docstring and a comment in Python?
- ๐บ Quick summary — Python comments and docstrings
Comments are like road signs that make a given code self-evident and highly readable. In Python, we can add single – and multi-line Python comments. This tutorial covers both approaches in detail. After reading this article, you will know how to add Python comments and which style to use.
Writing comments is a good programming habit. They are an unexecutable part of the code, but they are very important in the program. Not only can these help other programmers working on the same project, but testers can also refer to them to clarify white-box testing.
It is best to add comments when you create or update your program, otherwise you may lose context. Future reviews may not be as effective as they should be.
๐ช how to use single-line comments in Python?
When you need short, quick comments for debugging, you may prefer to use single-line Python comments. Single-line comments start with a pound sign (#) and automatically end with EOL (end of line).
Good code is self-documenting.
print("Learn Python Step by Step!")
Copy the code
# Define the month list
months = ['Jan'.'Feb'.'Mar'.'Apr'.'May'.'Jun'.'Jul'.'Aug'.'Sep'.'Oct'.'Nov'.'Dec']
Print calendar month function
def showCalender(months) :
The # For loop iterates through the list and prints the name of each month
for month in months:
print(month)
showCalender(months)
Copy the code
Copy the code
Return to the directory
๐ฐ how to use multi-line comments in Python?
Python allows comments to span multiple lines. Such comments are called multiline or block comments. You can use this annotation approach to describe more complex content.
This extended form of comment applies to some or all of the code that follows. This is an example of using multi-line Python comments.
๐ฌ 2.1 Use hash tags for comments
To add multi-line comments, each line should begin with a pound sign (#) followed by a space. You can divide your comments into paragraphs. Just add a blank line with a hash sign between each paragraph.
Note: The symbol (#) is also called octothorpe. The term comes from a group of engineers at Bell LABS who were working on the first touch-button keyboard project.
To learn any language, you must follow the following rules.
# 1. Understand basic syntax, data types, control structures, and conditional statements.
# 2. Learn error handling and file I/O.
# 3. Read advanced data structures.
# 4. Write functions and follow OOP concepts.
def main() :
print("Let's start to learn Python.")...Copy the code
Return to the directory
๐ฏ 3. How to use docstrings in Python?
Python has docstring (or docstring) capabilities. It provides an easy way for programmers to add quick comments to every Python module, function, class, and method.
You can define a docstring by adding it to a string constant. It must be the first statement in the object (module, function, class, and method) definition.
Docstrings are more extensive than Python comments. Therefore, it should describe what the function does, not how. In addition, it is good practice to have docstrings for all of your program’s functions.
๐ซ 3.1 Use triple quotes for docstrings
You can define docstrings with the help of triple quotes. Add one at the beginning of the string and a second at the end of the string. Just like multi-line comments, docstrings can overlap over multiple lines.
Note: Strings defined with triple quotes are docstrings in Python. However, it may seem like a general comment to you.
๐ก 3.2 What is the difference between a docstring and a comment in Python?
Strings that begin with three quotes are still regular strings, but they can be extended to multiple lines. This means that they are executable statements. If they are not marked, they are garbage collected once the code is executed.
The Python interpreter does not ignore them as it does comments. However, if such strings follow a function or class definition or a module, they become docstrings. You can access them using the following special variables.
myobj.__doc__
Copy the code
example
def theFunction() :
''' This function demonstrate the use of docstring in Python. '''
print("Python docstrings are not comments.")
print("\nJust printing the docstring value...")
print(theFunction.__doc__)
Copy the code
Return to the directory
๐บ Quick summary — Python comments and docstrings
Comments and docstrings add values to the program. They make your programs more readable and maintainable. Even if you need to refactor the same code later, it’s easier to use the available comments.
Only 10% of the software life cycle is spent on development; the other 90% is spent on maintenance.
Therefore, always place relevant and useful comments or docstrings, because they lead to more collaboration and speed up code refactoring activities.
If you enjoyed this article and are interested in seeing more of it, you can check out the source code for all my originals and works here:
Making, Gitee
- Python keywords, identifiers, and variables | Python theme month
- Python statements, expressions, and indentation | Python theme month
- 100 Basic Python Interview Questions Part 1 (1-20) | Python topic month
- 100 Basic Python Interview Questions Part 2 (21-40) | Python topic month
- 100 Basic Python Interview Questions Part 3 (41-60) | Python topic month
- 100 Basic Python Interview Questions Part 4 (61-80) | Python topic month
- 100 Basic Python Interview Questions Part 5 (81-100) | Python topic month
- 30 Python tutorials and tips | Python theme month
Recommended articles of the past:
- Teach you to use Java to make a gobang game
- Interesting way to talk about the history of JavaScript โ
- The output of a Java program | Java exercises ใ ใ 7 sets (including parsing)
- โค๏ธ5 VS Code extensions that make refactoring easy โค๏ธ
- It is said that if you have light in your eyes, everywhere you go is light
- 140000 | 400 multichannel JavaScript ๐ interview questions with answers ๐ items (the fifth part 371-424)
If you do learn something new from this post, like it, bookmark it and share it with your friends. ๐ค Finally, don’t forget โค or ๐ for support