CBSE CS and IP

CBSE Class 11 & 12 Computer Science and Informatics Practices Python Materials, Video Lecture

Comments (Single/Multiline) in Python

Comments (Single/Multiline) in Python


DataCamp

What is Comment in Python?

Any remark such as who made the code and the purpose in the form of a note can be added in the program using comments. Comments are not executed by the interpreter, they are added with the purpose of making the source code easier to understand by humans. The documentation of the code is done with the help of comments in the program itself. 

Comments are the additional readable information to clarify the source code, which is read by the programmers but ignored by the Python interpreter.

Why it is important to use Comments?

Whenever developers make a program, gradually so many codes are written in the same file for the different types of functionality. If commenting is not proper,  after some time, we do not understand our own code. We will face difficulty in understanding the codes written in our own program. So to easily understand the same problem, we use comments.
When you are working on a project with your team, it is necessary to use comments so that other programmers can understand what you have done in the source code by looking at the comment.
Using comments in our code makes our code easier to understand.

How do you write comments in code?

  • Comments are used to explain the code, which code is written for which work.
  • Comments are not part of the program, yet it is most useful for understanding the code.
  • Single line comments in Python begin with a symbol # (hash character) and generally end with the end of the physical line. (A physical line is the one complete line that you see on a computer whereas a logical line is the one that python sees as one full statement).
  • Multiline comments can be used in a program by simple put our text inside triple double-quotes (" " ") or triple single-quotes  (' ' '). Comments enclosed with triple quotes are called Docstring (Document String)

How many types of Comments?

Python has two types of comments, which are given below -:
  1. Single-Line Comments
  2. Multi-Line Comments

Single-Line Comments

In Python, Single line comments start with (#) symbol. 
If we put a # symbol at the starting of any line, then that line will be treated as a comment line. This line will not execute at the execution time, interpreter simply ignores it.
Example -: A single line comments in the program
# Single Line Comment
# Single Line Comment 

Multi-Line Comments

Multi-line comment in python code in two ways  -:
  1. Add a # symbol at the beginning of every physical line part of the multi-line comments.
  2. Type comment as a triple quoted multi-line string. This type of comment is also known as the docstring. You can either use a triple single-quote (' ' ') or triple double-quotes (" " ") to write a docstring. 
Example -: A Multi-line comments in the program
'''
This is a multiline Comment
This is a multiline Comment
This is a multiline Comment
'''
"""
This is a multiline Comment
This is a multiline Comment
This is a multiline Comment
"""


Benefits of using Comments in Python?

  1. The code can easily be understood when using comments.
  2. Makes code readable and understandable.
  3. Using Comments, we remember why we used this code.
  4. If the program is opened in the future, then there is no problem in understanding the code.
  5. Using comments gives the programmer an idea as to why they are using this code.


No comments:

Post a Comment