Comments in Python and its Examples

0
635

Introduction to Comments in Python

Comment means part of the code where you add extra information about your code. That comments in Python are not displayed in the code. Comment in Python starts with the hash(#) character

A comment can be written in three ways:-

  • Entirely on its own line.
  • Next to a statement of code.
  • And as a multi-line comment block.

Applications of Comments in Python

Comments make the code more readable.

  • Comments are used When working with any programming language, you include comments in the code to notate your work.
  • This details what certain parts of the code are for and lets other Programmer know what you were up to when you wrote the code.
  • This is a necessary practice, and good Programmers Majorly use the comment system to avoid any hassle of understanding of code.

How to Write Comments in Python

In Python, there are two ways to Write down comments for your code.

  • The first is to include comments that detail or indicate what a particular section of code means or does
  • The second makes use of paragraphs or multi-line comments which serve as documentation for others reading your code

Types of Comments in python

There are two types of comment in python

  1. Single line comments.
  2. Multi-line comments.

1. Single-line comments:-Are created simply by beginning a line with the hash (#) character, and they are automatically terminated by the end of the line

For example-

# This is a commentHere we write in the comment is “This is the comment”.

2. Multiline comment Multiline comment can be added by inserting a # for each line:

For example:-

# This is
# Data Science
# and Artificial Intelligence

Examples of Comments in python

 X=10     # here we declare a variable with integer 10

 y=20     # here we declare a variable with integer 20

 y=20     # here we declare a variable with integer 20.

 z=x+y    # here we use an addition operator to add two-variable x and y

# here we declare a variable with integer 10.# here we declare a variable with integer 20.

# here we use an addition operator to add two-variable x and yThese are the comments in the previous example with the help of these comments our anyone can easily understand the code.

Some more features in Python related to comments in Jupyter Notebook

  • Heading
  • Markdown.
  • Row NB Convert

Heading

In the heading section, we can give heading while we start a new project or topic in python. To give heading to a particular cell in Jupyter notebook steps are:-

1. Select the cell

2. Go to menubar

3. Dropdown the code menu Select the heading option and write the name of the heading.Or ESC+1,2,3 followed by the heading size.

Here is the practical example in the jupyter notebook

Here is the practical example in the Jupyter notebook

Step 1:-

Comments in Python in Jupyter Notebook

Markdown

Markdown is used to write information or definition related to the function syntax.

To give markdown to a particular cell in Jupyter notebook steps are:-

Select the cell > Go to menubar > Dropdown the code menu select the markdown option.

RowNb ConvertRowNb

convert is used to write a syntax and some structural data which is not executable but the structure remains the same at the condition we use RowNB convert

To give RowNb to a particular cell in Jupyter notebook steps are:-

Select the cell > Go to menubar > Dropdown the code menu > Select the RowNb option

Conclusion

This Blog will help you to get a better understanding of Python comments and there uses

LEAVE A REPLY

Please enter your comment!
Please enter your name here