As a programmer, we deal with so many blocks of codes every day! Sometimes we get solutions at once, or sometimes not. While doing coding, we perform various kinds of Syntax formatting that make coding complex, and because of it sometimes becomes hard to memorize the use of a particular block of codes. In this case, what we want to do is that we want to write a few lines between a bunch of code lines, as per need, to remember the use of a particular code line and not want Python to read that line as a part of the main block of codes.
Did you ever think how difficult it would be to remember using a particular block of codes when you are already inside the deep ocean of codes?
That’s where Python Comments comes in role.
Before moving ahead, let’s know a little bit about Python’s first program Python Syntax Concept
Table of Contents
What is a Comment?
Comment in Python is considered an unnecessary line not read by Python Interpreter while running a Python file. It helps programmers & makes it easy to keep remember the use of a particular block of code when there are already thousands of blocks of code.
How to use Comment?
Comment in Python is denoted by the sign Hash (#). Python allows us to write comments anywhere in the file using sign #. Make sign # at the starting line, and whatever you write in that line will be considered Python Comments.
Windows – Ctrl+ /
Mac – cmd + /
To get back Comment as code, use the same shortcut.
Why should I use Comment?
Comment helps programmers to make code more readable & understandable while writing a block of codes. It makes it pretty easy to go through each block of codes to understand them. There’s no limit to comments in Python; you can write as many as you want.
Create a comment
# This line is a comment line.
Comments as Code
Comment in Python is not mandatory as a text line; it can be a code.
#print('This is a comment line!') print('I love to learn on codingstreets.com') print('This is not a comment line')
Code as not comment
You can also use Comment at the end of the line to make a code simple to understand, but Python will not consider that line as Comments.
Print (“Hello World”) # This is not a comment line since the Hash (#) sign is used after the code is written.
What are Multi-Line Comments?
Multi-Line comments are defined as writing more than one Comment or statement. In Python, you can define multi-line comments in two ways; (i) adding sign (#) at starting of each line, and (ii) enclosing a line within either single triple quotes (”’) or double triple quotes (“).
Create multi-line Comment
Use sign (#)
# This line is a comment line. # This line is the second Comment. # This is also comment and not considered as main code in Python.
Use single triple quotes (”’)
''' This line is single triple quotes considered as Python comments which is written in multi-line. '''
Use double triple quotes (“””)
""" This line is double-triple quotes considered as Python comments which is written in multi-line. """
Variable is not comment
If you define a comment and store it in any variable, then Python will not consider that Comment as a comment and will be considered a code.
# This line is a comment line. It will not be printed. txt = 'This line is not a comment line. It will be printed.' print(txt)
As a result, it returned the print statement because it was stored in a variable named txt.
Take a look at what you’ve learned so far!
— What is Comment?
— How to use Comment?
— Why should use Comment?
— Create a comment
— What are Multi-Line Comments?
— Create multi-line Comment
— Variable is not comment
As we’ve discussed in deeply on Python Comments, it made clear your doubts & confusion regarding Comments in Python. We hope you enjoyed it!
Happy Coding 😊