Which Character is Used in Python to Make a Single Line Comment?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Which Character is used in Python to make a Single Line Comment?

In technical terms, comments are lines of code or statement that the interpreter or Python compiler ignores during execution. These are used to write some programmer readable information/explanation about the code, which helps other programmers understand the code.

Python offers a pretty simple and straightforward way to write a comment. We can write a single-line comment by adding a single # character before any statement or line of code.

Writing Single-Line Comments

See this basic example to know how comments are written in Python.

Code:

Output:

Digression: We can add multi-line comments in python by wrapping the string of comments in triple quotes.

These multi-line comments are generally used to write long comments conveniently because it avoids writing a # character on each line.

There is a term docstring in python, which stands for documentation string. It is used to describe any module, function, or programming component. These docstrings in python are nothing but multi-line comments.

Code:

Output:

Explanation:

  • The output of the program conveys that the interpreter ignores lines from 3-5.

Examples

Add two Numbers with Comments.

We have already seen how to write comments, let's discuss how comments are used to describe some information about the code.

Code:

Input:

Output:

Explanation:

  • You can see how we are writing some information about the code with the help of comments.
  • These comments are also getting ignored by the interpreter.

Add a Single Line Comment after Another Statement

It is important to note that only the characters written after # are considered as comment.

Code:

Output:

Explanation:

  • As you can see in the following example, the entire line is not considered a comment, hence interpreted by python.

Why Comments?

We have already discussed the comments and their examples, let's see why there is a need of such concept of comments in programming.

  1. Since programming can be pretty complex, if two or more programmers work on the same program, one might have difficulty understanding the code written by the other. Hence, comments help in understanding the program by describing the information of some lines of code in English or human-readable language.
  2. Programmers often comment on code instead of deleting it to prevent that code from being executed. This technique makes debugging easier, and also those commented lines can remain in the program code without affecting its overall working.

Conclusion

  • Comments are used to write some information about the code.
  • The compiler or interpreter ignores comments during the execution of a program.
  • We can use the # character to write the comment in python.
  • All the characters written after # are considered comments.