Comments in Python

python-comments

What are comments

Like other programming languages, Python also support commenting codes. Comments are not read or executed as part of the program, rather they are used to add notes / explanation in code.

Single-line comment in Python

Just add # at the beginning of the line.

Example

1
2
# This is a comment line.
print("Ravi Shekhar") # we can also add comment after code.

Multiple-lines comments in Python

There are two ways you can write multiple lines codes.

  1. Put # at the start of every line.
  2. Using triple quotes.

How to use triple quotes for multiple lines comments

Just wrap your comment inside a set of triple quotes.

1
2
3
4
5
"""
This is first line of multiple lines comment.
This is another comment line.
This is third comment line.
"""
1
2
3
4
5
'''
This is first line of multiple lines comment.
This is another comment line.
This is third comment line.
'''

How to add # at the start of multiple lines by one click

Select the lines that you want to comment (add # at the start of the line) and press:

  • On Windows: Ctrl + /
  • On Mac: cmd + /

I hope this python tutorial was helpful to you.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *