0% found this document useful (0 votes)
26 views

8.python Comments

The document introduces the different types of comments in Python - single-line comments using #, multi-line comments using multiple # on separate lines, and docstrings enclosed in triple quotes to document functions and classes. Comments make code more readable and understandable by explaining code sections, parameters, and logic. They can also be used to disable code execution during testing.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

8.python Comments

The document introduces the different types of comments in Python - single-line comments using #, multi-line comments using multiple # on separate lines, and docstrings enclosed in triple quotes to document functions and classes. Comments make code more readable and understandable by explaining code sections, parameters, and logic. They can also be used to disable code execution during testing.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Python Comments

Introduction to Python Comments


• We may wish to describe the code we develop. We might wish to take
notes of why a section of script functions, for instance. We leverage
the remarks to accomplish this. Formulas, procedures, and
sophisticated business logic are typically explained with comments.
The Python interpreter overlooks the remarks and solely interprets the
script when running a program. Single-line comments, multi-line
comments, and documentation strings are the 3 types of comments in
Python.
Advantages of Using Comments
• Our code is more comprehensible when we use comments in it. It
assists us in recalling why specific sections of code were created by
making the program more understandable.
• Aside from that, we can leverage comments to overlook specific code
while evaluating other code sections. This simple technique stops
some lines from running or creates a fast pseudo-code for the program.
Below are some of the most common uses for comments:

• Readability of the Code


• Restrict code execution
• Provide an overview of the program or project metadata
• To add resources to the code
Types of Comments in Python
• Single-Line Comments
• Single-line remarks in Python have shown to be effective for
providing quick descriptions for parameters, function definitions, and
expressions. A single-line comment of Python is the one that has a
hashtag # at the beginning of it and continues until the finish of the
line. If the comment continues to the next line, add a hashtag to the
subsequent line and resume the conversation. Consider the
accompanying code snippet, which shows how to use a single line
comment:
Code
# This code is to show an example of a single-line comment
print( 'This statement does not have a hashtag before it' )
print( 'This is not a comment' ) # this code is to show an example of a si
ngle-line comment
Multi-Line Comments
• Python does not provide the facility for multi-line comments.
However, there are indeed many ways to create multi-line comments.
• With Multiple Hashtags (#)
• In Python, we may use hashtags (#) multiple times to construct
multiple lines of comments. Every line with a (#) before it will be
regarded as a single-line comment.
# it is a
# comment
# extending to multiple lines
Python Docstring
• The strings enclosed in triple quotes that come immediately after the
defined function are called Python docstring. It's designed to link
documentation developed for Python modules, methods, classes, and
functions together. It's placed just beneath the function, module, or
class to explain what they perform. The docstring is then readily
accessible in Python using the __doc__ attribute.
Code
# Code to show how we use docstrings in Python

def add(x, y):


"""This function adds the values of x and y"""
return x + y

# Displaying the docstring of the add function


print( add.__doc__ )

You might also like