► Play

What is comments?

“Comments are the lines that are non-executable in programming languages. Humans or Programmer can easily understands the comments but compiler not understands these statements. Comments in python language is the way to increase the reliability of the code for programmers.”

One Line Comment

One line comment means comment only single line.

Syntax in Python:

We # (Number Sign) operator for single line comments in Python programming language.

# write your comments here.

Multiline Comments

Multiline lines comments means comments multiples lines or comment a block of lines.

Syntax for Multiline in Python:

There are two ways for multiline comments in python

We can use three single quotes (‘’’) before the block and after the block.

‘ ’ ’ (Start Comments)

Write your comments lines here

Write Something

‘ ’ ’ (End Comments)

Also we can use three double quotes (“””) before the starting lines and close again three double quotes in end of the comments lines.

“ ” ” (Start Comments)

Write your comments lines here

Write Something

“ ” ”(End Comments)

Comments in Python 3.x (3.7)

Python 3 also provides a facility for single or multiline comments. In python 3 we can comments single line and multiline comments. The syntax and explanation for these already mentioned above.

Shortcut Keys for Comments in Python

Python compilers also provides the shortcut keys for single line and multiple lines comments.

Keys. First select the lines which need to be commented.

Ctrl + 1

Control plus one used for Single line comments in python.

Ctrl + 4

Control plus four used for multiple lines or block comments in python.

Ctrl + 5

Python also provide shortcut key control plus five for uncomment the single or multiple line comments in python.

Comments in Python IDLE

IDLE is integrated development environment (IDE) for editing and running Python 2.x or Python 3 programs.

Single line and multiple line comments syntax is same in python IDLE. But shortcut keys are different.

If you want to comment out a block of code, then select the lines which need to be commented on Python IDLE editor and press Alt+3.

Similarly for uncommenting a region or block of code, select the lines and press Alt+4.

Comments in Python Script

We know that python is a programming and scripting language in scripting language also we need comments for better understanding of programmer or coder like javaScript is scripting language. In python we can add comments like single line comments and also multiline comments. The syntax is same that is already given above.

How To Comment In Python

  • Commenting single line:

Add # (hash symbol) before the start if line.

  • Commenting a block with docstring support:Add ”’ (three double quotes) at both ends.
  • Commenting a block without docstring support:Add ‘‘‘ (three single quotes) at both ends.

Comments are used to describe the code in human language in order to make it easier to read and understand—not only for other programmers, but also for your future self. They are ignored by the computer.

In Python, hash (#) is used to start a comment:

# This is a comment

# The line below writes ‘Hello World’ in the console.

print(‘Hello World!’)

Comment can also be added in the middle of the line. Everything between the hash sign and the end of the line will be a comment.

def some_function(): # This is some function.

How To Comment In Python

Python is an interpreted general programming language which has grown to become one of the most widely used programming languages due to its simplistic code, ease of use and plethora of available libraries. Many new students are adopting python as there first general programming language due to its popularity and simplicity.

While working with any programming language developers need to describe certain parts of their code so that they themselves and any other person who works on that piece of code understands what the code does and how the original developer intended it to work.

This is a necessary practice as even simple projects can contain thousands of lines of code with multiple people interacting and modifying the code, throughout the lifetime of the project.

Comments are special lines between code snippets in a program that are not evaluated by the compiler or interpreter. They do not make the program run fast or slow but impact the readability, reusability and maintenance of our code.

We can add single line comments in Python simply by beginning a line with hash(#).

For Example:

We can also write multiple line comments simply by enclosing our comment between Three inverted commas (“””). Multi line comments allow us to describe our code in more detail without beginning every line with hash (#).

For Example:

A good practice is to write a header above every function header as to what parameters the functions accept, what does the function do with those parameters and what is the return value of the function.

Avoid writing long winded comments describing every line of the code. Comments should be concise and to the point but should deliver the necessary information about the code to your future self or any future developer else who works on it.

Send us a feedback0/500

Do you like this article?