Comments are a handy tool for programmers and considered as one of the best practices for developers. Even though comments will not alter the execution flow or change the outcome of the code, it improves the readability of the code and helps you to understand why a particular block of code was written at a later point. In this article, we will learn about Python Comments, both Single line, and Multi-line comments.
Why Are Comments Needed?
We know that Comments will not alter the execution flow or change the outcome of the code, but why is it needed?
As the program gets bigger and more complicated, they get more challenging to read, and it is often difficult to look at a piece of code and figure out what it is doing, or why. For this reason, add comments to your program to explain in natural language what the program is doing.
They come in handy when someone else is analyzing your code for a bug fix or implement a new logic, just by reading your comments they should be able to understand your code better rather than going through the entire code.
Comments serve the below two purposes.
- It explains how the program works, which helps others to understand the sole purpose of your code.
- It allows you to temporarily skip certain parts of the program so you can test other parts of the program.
Comments are non-executable statements, neither the Python compiler nor the PVM will execute them.
There are two types of comments in Python:
- Single line comments
- Multiline comments, 0r Block comments
Single line comments
This comment starts with a hash symbol (#), this mentions the compiler they should treat the entire following line as comments
# Initializing the message message = 'Welcome to JavaInterviewPoint'
Here the first line is starting with a #, and hence the whole block gets treated as a comment.
You can also add comments inside a code, like below.
a = 15 # Store value 15 into variable a
Here, comments appear after the statement a =15, # symbol defines the start of the comment describing “Store value 15 into variable a”
Multiline comments (or) Block comments
Multiline comments are useful to explain complex codes,
When we need to mark multiple lines as comments, then adding # at the beginning of each line be a tremendous job.
# This is a program reads the input from the user # and performs the addition operation on the input values
Instead of starting every line with the # symbol, we can write the previous block of code, we can write the previous block of code inside “”” (triple-double quotes) or ”’ (triple single quotes) in the beginning and end of the block as below
""" This is a program reads the input from the user and performs the addition operation on the input values """
''' This is a program reads the input from the user and performs the addition operation on the input values '''
The triple-double quotes (“””) or triple single quotes (”’) are called as ‘multi-line comments’ or ‘block comments’.
If we write the block comments as first statements in a module, function, class, or a method, then these strings are called documentation strings or docstrings. These docstrings are useful to create an API documentation file from a Python program.
Unlike other programming languages, Python by default will not support Multiline comments and the recommended approach to comment out multiple lines of code in Python is to use consecutive # single-line comments
Leave a Reply