What are the two types of comments in Python? How are they written?

Answer:

Answer by student

The two types of comments in Python are single-line comments and multi-line comments. Single-line comments are written by using a # symbol at the beginning of the comment . Multi-line comments are written by using triple quotes (‘’’ or “”") at the start and end of the comment.

Detailed answer by teachoo

-lock-

The two types of comments in Python are single-line comments and multi-line comments. Comments are parts of the code that are ignored by the Python interpreter and are used to explain or document the code.

  • Single-line comments are written by using a # symbol at the beginning of the comment . They can be placed on a separate line or at the end of a code line. They can only span one line and cannot be continued on the next line.
  • Multi-line comments are written by using triple quotes (‘’’ or “”") at the start and end of the comment. They can span multiple lines and can be placed anywhere in the code. They can also be used as docstrings to document functions, classes, or modules.

For example, if we want to write some comments in our hello.py file, we can write:

[Sample Paper Class 11] What are the two types of comments in Python? - Sample Paper 1 by Teachoo

-endlock-

Remove Ads
Teachoo · Class 11 Explore Class 11
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.