Comments in programs can be used for different purposes. These include the typical 3 following variants:
- explanatory comments within the code
- better readability of the code
- for testing code, resp. to prevent the execution of commented out parts of the program
If a program becomes more complex and you maintain it for years resp. several people work on one software, it is helpful to comment sections of code. This way, even years later, it is quickly clear why something was implemented in a certain place in the corresponding way.
Comments can also help when programming in teams.
As a beginner, however, you are more likely to benefit from the following type:
better readability of code
The source code can be made more readable by comments accordingly. Python already forces very clean code through indentation. However, depending on the application, a comment can further improve readability.
comment out to test code
If a program part does not work as desired, existing code can quickly be commented out and test code can be inserted for testing purposes. Commented-out code can be quickly re-enabled after the bug is found. This has against delete the big advantage that everything is available again and nothing is forgotten accidentally.
Possibilities for commenting/commenting
Comments start with a hash. Everything after the double cross "#" is ignored by Python.
Comments can also be placed after commands. From the double cross "#" everything is ignored!
If a code is to be commented out ("turned off"), the double cross is simply placed at the beginning of the line:
As a result, we only get the word "world" as output. The line with the output for "Hello" is completely ignored by the comment character.
Comments over multiple lines
Most of the time, you don’t want to comment out just one line of code, and you don’t want to place a double cross in front of every single line of code either. Then the possibility offers itself to comment out several lines at one blow.
This is done with 3 double quotes:
At the end there are again 3 double quotes. From now on all Python commands will be executed again.
This procedure can be used to place extensive comments as well as to comment out entire program sections:
Recommend- Social Bookmarks- Thanks a lot
Please support this project
You can support this project in different ways – we would be happy and it would motivate us for more content :).
Recommend it to others – we are always happy to receive links and Facebook recommendations.
The new E-Book for the course is available for purchase.
You can also make a donation send via PayPal.
Order books about the following Link at Amazon:
books about Python
Many thanks for your support
- buy an e-book
E-book with about 500 pages as PDF. With introductory price until the table of contents gets page numbers.