In this lesson, we’ll look at different ways for “Commenting a Python Code | Comments in Python”.
Comments are the user-defined statements that do not effect the normal flow of execution of a program. They are added to increase the readability and understandability of a program. In Python programming, comments are defined with the help of ‘ # ‘. Comments are generally ignored by the interpreter.
There are two ways for commenting a python code :
1. Single-Line Comment : These comments start with a ‘#‘ symbol
For instance, let us consider the following code :
a=10
b=20
# storing the sum value in variable c.
c = a + b
print(c)
This code will print the sum of two numbers (a, b) i.e 30. In the above code, line 3 is acting as a comment and it will not have any effect on program execution/ result.
2. Multi-Line Comment : In multi-line comments, we use ”’ (triple quotes) or ” (double quotes) in the beginning as well as at the ending of the block.
This code will print nothing as we have commented statements line 3 & line 4.
a=10
b=20
'''c = a+b
print(c)'''
Do you know ?
Multi-Line comment is nothing but a string literal which has not been assigned to a variable. Hence it is ignored in Python.
Quick Question
Do multi-line comments occupy space ?
That’s all, see you in the next lecture.
