0% found this document useful (0 votes)
11 views8 pages

21CSS101J U4 S4 SLO1 Single Line and Multiline Comments

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

21CSS101J U4 S4 SLO1 Single Line and Multiline Comments

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 8

21CSS101J-Programming for

Problem Solving

U4-S4-SLO-1-Python-Single
and Multi Line Comments
Comments in Python
• Comments are descriptions that help programmers better
understand the intent and functionality of the program.
• They are completely ignored by the Python interpreter.
Advantages of Using Comments
• Using comments in programs makes our code more
understandable. It makes the program more readable
which helps us remember why certain blocks of code
were written.
• Other than that, comments can also be used to ignore
some code while testing other blocks of code. This offers
a simple way to prevent the execution of some lines or
write a quick pseudo-code for the program.
Why are comments beneficial during coding?
• Future Reference - Assume that you have written a program
which contains several hundred lines of code without any
comments. If you open the program after one or two months,
then you will find it challenging to understand your code.

• Comments help to understand the code of others - We


usually work in a team structure, where others review the code
of one team member. Also, you might need to work on a
feature that was earlier worked by some other developer.

• Code Debugging - Sometimes, we have to check a part of the


code to see whether it's working as expected or not. In this
case, we can comment on the remaining code.
How to write comments in Python?

• In Python, any statement which begins with


a hash (#) symbol is a comment.

• Any code you write after the hash (#) symbol


doesn't get executed.
Types of Comments in Python
• Single-Line Comment
• Multi-Line Comment
Single-Line Comment
• In Python, we use the hash symbol # to write a
single-line comment.
Multi-Line Comment
• We can use # at the beginning of each line of
comment on multiple lines.
• Each line is treated as a single comment and all of
them are ignored.
Single-Line Comment
• A single-line comment begins with a hash (#) symbol
and is useful in mentioning that the whole line should
be considered as a comment until the end of the line.
Example:
#Defining a variable to store number. n = 50
#Store 50 as value into variable n.
• In the above example program, the first line starts with
the hash symbol, so the entire line is considered a
comment.
• In the second line of code, "N = 50" is a statement, and
after the statement, the comment begins with the #
symbol. From the # symbol to the end of this line, the
line will be treated as a comment.
Multi-Line Comment
• Multi-line comment is useful when we need to comment on
many lines. You can also use a single-line comment, but using
a multi-line instead of single-line comment is easy to comment
on multiple lines.
• In Python Triple double quote (""") and single quote (''') are
used for Multi-line commenting. It is used at the beginning
and end of the block to comment on the entire block. Hence it
is also called block comments.
Example:
""" Python program to demonstrate
multiline comments"""
print("Multiline comments")
Python Docstring
• Python Docstring is the string literals with triple quotes that are
appeared right after the function.
• It is used to associate documentation that has been written with
Python modules, functions, classes, and methods.
• It is added right below the functions, modules, or classes to describe
what they do.
• In Python, the docstring is then made available via the __doc__
attribute.
Example:
def multiply(a, b):
"""Multiplies the value of a and b"""
return a*b
# Print the docstring of multiply function
print(multiply.__doc__)

You might also like