How to write comments in python 3?

comment in python

The comments are known as the strings that help to explain the code. The comments in python is one of the important parts of programming. A good coder always uses the comment features for the best identification of the code while coding. Including code comments is a good habit ever to easily guess the activity and work of that specified code. The comments are helpful for the coder and others to easily understand self and others. The comments are helpful for the coder and others to easily understand. The comments in python 3 is more useful when we revising or testing our code after a long time.

Some of the importance of comments in python 3

The comments are helpful for the beginner to easily understand and converting their idea into codes. When you forget everything or information about every line of code in the program, the comments will be very helpful at the time. Moreover, when you share your program with other developers you need not have to explain the flow of the program. For example, take any kind of project. Every should be creating or programming with a specified code of the programming language. But no all the consumer or customer knows the programming, because they are well worst in some other field. They need a project that should be in the code of programming language. But the customer knows the English language only. In that time these comments of the program will be helpful for the people to understand. Comments are known as the explanation part of the program, which helps to explain the work flow and program. To understand deeper about the comments in the python 3 program, it is a very useful and important part for the coder to explain the flow of a program. Python had a specified interpreter, that would not be interpreted with the comments as a part of the program.

Including the comments in the programming will be the best option to make the code more readable and user-friendly. The comments in the program will be ignored by the compiler and interpreters. It is known as the information or explanation about the code understandable by the human. You can create multi-line comments single line comments included inside the function are considered as a documentation string to explain the work and data flow of that particular function or method. You can create multi-line comments single-line comments for a variable declaration to explain the data storage process. In some cases, multi-line comments are written as a documentation string that can be printed out as an output, if you use the statement as an output. There are four different ways are available for the user to use the comments in python 3 which are block comment, inline comment, multiline comment, documentation string, and commenting out code.

Syntax of the comment in python

Comment will be denoted by using the symbol called hash (#). To use the comment in the python program use hash symbol (#) and give a single space and continue the line with character and string. The different programming languages will have different symbols to denote the comment in the program. Likewise, the comment in the python 3 will be written as follows. When you run the program or specific code you will not know the exact work of the comment in the output. It will be only placed in the source code, not executed at the end of the program.

# This is a statement

Example

# This is a statement, which helps to print the sentence called welcome to our country

Print(“welcome to our country”)

Here is an example to understand the comment. When you are creating the code for specific work of the statement in the program. It may not be understandable by the beginners or the readers. If you use comments like the above given is easy to access and test. This will not ever be shown in the output or console whether you executing the program. Because this could not be considered as a code by the interpreter and compiler. Comments only readable in the source code and only usable for the humans, not for the computer to evaluate, calculate, or execute.

Single comment with example

If you writing the code to display the sentence welcome to python. To creating the code with the comment you just type the definition with the hash symbol is given below.

# print the statement called “welcome to python” in the console

Print(“welcome to python”)

Here is an example to understand the single comment in python. If you are creating the two different functions for adding two numbers called def add and subtract two numbers called def sub. Each of the functions should be having the parameters. Few of them even do not know the actuation work of the two different functions, in that case using the comments is better for explaining the program.

# This function passes two different arguments inside the function

def add (x, y, z)

# Add two value of the variable

z = x + y

return z

# Display the sum of the variable

print (‘The addition of x and y is z’. format (x, y, z))

# This function passes two different arguments inside the function

def add (x, y, z)

# Subtract two value of the variable

z = x – y

return z

# Display the sum of the variable

print(‘The difference of x and y is z’. format (x, y, z))

These above-given functions are known as the different. But beginners do not have much capacity to find the difference. When you are adding the comment for those functions, they are easily finding the difference between the two functions.

Block comment in python 3

Block comments are generally used to explain some of the code or all the codes that are applicable in the program. These codes are indented to the same level as that code. Each of the block comment will be starts with a # and a single space. If you want to display the sentence called start learning python. You can write code that is understandable by the interpreter or compiler. You can also explain the sentence using comment which is helpful to understanding for the human.

Here is an example to understand the block comment in python.

Example for block comment

# print start learning python

Print(“Start Learning Python”)

It will be given just like a state but this will be the important one for each beginner to understand the code without getting any help from others. This also helps to solve the problem from the code easily. These comments will not be taking much time to create. It is simple and very useful in the future. Block comments are used to explain the complicated code or code that is not known to the reader or not familiar with the reader.

Inline comment

An inline comment is also known as a comment which is declared on the same line as a statement. Inline comment will be created by using two spaces from the statement to denoting the special comment. The inline comment will be declared followed by the python code. Each of the inline comments will be starts with a # and a single space. If you want to display the sentence called learning python program is very exciting. You can write code called learning python program is very exciting followed by the print option, which is understandable by the interpreter or compiler using the print statement. It is not necessary for all the statements, and it is best over leaning the comment. Here is an example to understand the inline comment in python.

Example

Print(“Learning python program is very exciting”) # printing learning python program is very exciting

Multiline comment in python

Multiline comments are starting with the triple quotes, not within the single codes like block comments. Python interpreter ignores the multiline comment and it would not be printed in the output or result of the python program. This will only be used as the reference process or unrewarding process. You can use either single triple quote like (‘’’) or double triple quotes like (“””) for declaring the multiline comments in python. For online and multiline comment, be careful to keep the closing “”” on the same line. Once you close the multiline comment using the single or double triple quotes only it will work correctly. Otherwise, it will show the error message. Here is an example to understand the multiline comment in python.

Example

“”” This program will help to declare and pass two different arguments inside the function, and multiply two value of the variable and stores in the variable called z, display the multiplication of the variable called x and y “””

Def multi (x, y)

z = x * y

return z

print(‘The multiplication of x and y is z’. format (x, y, z))

output

The multiplication of 3 and 5 is 15

When you are using multiline comments for more than one line expiation, it will help to reduce the size of the program. You can understand the text inside the triple quotes is not executed by the interpreter. The statement inside the print will be displayed as the output of the program. This is how you write the multiline comments in python easily to understanding the program. You can also use the multiline comment technique to comment out a block of code when not necessary to execute.

Documentation string in python

The program called the documentation string which is a multiline comment that can be printed as output. It is an optional first statement of the function body which are written as the comment line. The documentation string in python could be written inside a triple quote and it can be printed on the output screen with a special method called --doc--. Documentation sting will be helpful to explain the scope of the variable like private, public, protected, about the modules, functions, class, and methods. Documentation strings are not necessary for nonpublic methods, if you want to create a comment or documentation string for nonpublic methods there is no restriction made. But you should have a comment that describes the work of the methods. This comment should be used after the def line. Here is an example to understand the documentation string comment in python.

Example

Def cal(x, y)

“”” This is a documentation string about function call. This function will help to do a simple arithmetic calculation such as addition, subtraction, multiplication, and division “””

addition = x + y

subtraction = x – y

multiplication = x * y

division = x / y

return addition, subtraction, multiplication, division

a, s, m, d = cal(10, 5)

print(” Addition: {a}, Subtraction: {s}, Multiplication: {m}, Division: {d}”)

print(cal.__doc__)

Output

Addition: 15, Subtraction: 5, Multiplication: 50, Division: 2

This is a documentation string about function calls. This function will help to do a simple arithmetic calculation such as addition, subtraction, multiplication, and division

The above given is the best example to display the multiline comment in python.

How to use a python comment?

Comments are used to prevent the coding and execution process of the code. Comments can be written in the python code for different purposes such as make it easy, understandable, readable, flexible, and explanation. Depending on the purpose of your program, programs can be created to yourself make it easier. You can make your code more readable to yourself as well as for the person or other who are interesting to read your code of reader or developers with comments.


Learn more :

How to use Putty? All Windows and Linux tips and tricks!

Install Java with Apt on Ubuntu 18.04

Start now for as low as $4,99 a month!

  • 8 Characters2
  • 1 Uppercase Atleast
  • 1 Number Atleast