
Python Comments
In this online lesson, I will explain how to insert comments in a Python script and why they are useful.
What are comments? Comments are parts of the code where you can explain what you are doing. They are not processed by the Python interpreter. However, comments are useful for reminding yourself or other programmers who will modify the program in the future how the program works.
Here's a practical example.
This script adds two variables and displays the result.
a=2
b=3
c=a+b
# Prints the result to the screen
print(c)
Python executes the first three lines and the last one.
It does not interpret or execute the fourth line because it is a comment. The output result is 5.
5
You can also insert comments at the end of a line of code.
a=2
b=3
c=a+b # c is the sum of a and b
print(c)
In this case, Python executes the code until the hashtag.
When the Python interpreter encounters the hashtag symbol, it moves on to process the next line of code.
How to insert a multiline comment?
Unfortunately, the Python language does not have a specific syntax for multiline comments.
However, to write a comment on multiple lines of code, you can use the hash symbol at the beginning of each line.
a=2
b=3
c=a+b
# A practical example of a comment
# written on two consecutive lines of code
print(c)
If you like this lesson from StemKB's Python course, please continue to follow us.