Introduction to Comments in Python
Comments in Python are a systematic technique for defining the program’s functional flow in the simple, readable text so that it makes it easy for instructing the person who executes the program and to describe what can be expected from the specific snippet of code. It is very important not to overdo the comments, as it creates high chances of influencing the program’s overall size in terms of the number of lines in the program and the memory required to save or execute the program. In Python, comments are added after ‘#’, if it is a single line comment and triple quotes(“) for more than one line comment.
Types of Syntax in Python
In Python there are two types of syntaxes used for comments:
#: Is used to comment on one line.
Triple quotes(""" """): To comment more than one line.
In all programming languages, some syntax has been defined to identify what is what. A program is defined with a number of syntaxes for e.g., closing a line in c, we use a semicolon ‘;’, To define starting of a class or loop, we use colon ‘:’. As can be seen, we have a different syntax which is the part of the code and affects it. There should be some syntax that could say complier to leave commented part and go to the next step, as comments don’t have anything to do with the output. So, it is really necessary to distinguish the comments from the actual code to not be confused with the actual program.
Creating Comments in Python
A programmer must add a comment in his program to make it more understandable. Now where to add comments? It’s an individual’s choice, but here are some common places where adding comments makes a program more readable.
- Start of the program
- When defining parameters
- Defining a class
- Defining methods
- Logic for the methods/ functions
- Description about the procedure
- To cross-check the output of a piece of code
The following points have been explained below:
1. Start of the Program: Here a programmer defines what the problem statement is and what is the need of the program.
2. When Defining Parameters: Here a programmer can add comments for each parameter, what it stands for, how it will help in the program.
3. Defining a Class: A program may contain null/ one or more classes, so it’s essential to define the purpose of the class at the beginning of the comments. Sometimes, it is advised to add comments in between class to define each line’s job in class.
4. Defining Methods: Methods are functions that create relations among parameters and logic for manipulating them. It is recommended to define the job of the method in the comments.
5. The Logic for the Methods/Functions: In general, a method can have a number of logics attached to it, commenting description of each logic can help understand existing logic, and if needed, can be modified as per the requirement.
6. Description Of the Procedure: For each program, a programmer follows a step-by-step procedure, and defining the steps of procedure/ Architecture of the code in comments could help improving its readability.
7. To cross-check the Output of a Piece of Code: In a number of cases, a programmer would need to make changes to the existing program. If a program has a number of logics and to cross-check whether all are working fine or not, he should have reference output/ Expected output. Commenting on the outputs of that logic just after logic could save a lot of time.
Examples of Comments in Python
The examples are given below:
Comments don’t have any effect on output what so ever, but they make a program more readable. Here are some examples of comments in python.
1. Commenting one line at a time: Here, we are performing an addition operation on two variables.
# Performing addition of two variables a = 12 # defining variable a and its value b = 10 # defining variable b and its value addition = a + b # Addition logic definition print(addition) # getting output
2. Commenting multiple lines at a time: Sometimes, it is required to provide a description of some logic, for which there is a need to comment multiple lines at a time. Commenting a single line, again and again, could be a tiring job, so we use triple quotes for this.
""" This lines are commented to perform addition task We will define two variables we will apply addition logic we will print the output """ a = 12 b = 10 addition = a + b print(addition)
3. Commenting on one line as well as multiple lines at the same time: In most cases, a programmer uses both types of comments to make the program more readable.
""" These lines are commented to perform addition task We will define two variables we will apply addition logic we will print the output """ a = 12 # definng variable a and its value b = 10 # defining variable b and its value addition = a + b # Addition logic definition print(addition) # getting output
Here we have seen some cases of comments, which are used in a program. In the case of Machine learning, there are different types of models, which are used to predict the output. One need to model parameters, hyperparameters, and procedure used in the algorithm. Commenting on different approaches for the model with different parameters can save repeatability and can save time.
Conclusion – Comments in Python
Comments in a program are very useful in understanding them. In python, we basically have two types of comments either to comment one line with (#) syntax or to comment multiple lines; we use triple quotes (“). Comments are not there to influence the code but just to add a description of what is being codded there, and Comments would not be part of the output.
This is a guide to Comments in Python. Here we discuss the introduction, creating comments in python have taken into consideration, examples, and syntax, etc. You can also go through our other suggested articles to learn more –