Comments In Python

A comment is a text within a program that is not executed (ignored by compiler and interpreters). It can be used to provide additional information to aid in understanding the code.

Using comments in programs can improve code readability for humans by providing information or explanations about what each part of a program is doing. In general, it is a good idea to write comments.

⇒How to Write Comments?

Comments in Python start with a hash sign (#) and a whitespace character and continue to the end of the line.

Comments look like this:

  • #This is an example of a comment in Python.

Result:

  • #Print “Hello, World” to console

print(“Hello, World”)

Result: Hello, World

  • print(5 + 10)

x = 10 * 2

print(x)

#This is a comment

Result: 15

20

As you can see from above code, the interpreter will run the first three lines and skip the comment.

⇒Types of Comments:

  • Inline Comments / Single-line Comments

An inline comment exists on the same line. You can create an inline by adding the hash (#) sign.

  • E.g. x = 5.12   #Assign the float to a variable x

print(“Usman”)   #Print the word Usman

  • print(“This will run.”)  # This won’t run
  • border = x + 10  # Make offset of 10px
  • Multi-line Comments

Python does not really have a syntax for multiline comments. To add a multiline comment, you could insert a # for each line:

  • E.g. #This is a comment

               #written in

               #more than one line

                   print(5)

  •    # So you can’t

                  just do this

                   in python

    In the above example, the first line will be ignored by the program, but the

     other lines will raise a Syntax Error.  

  •       # This is a pretty good example

      # of how you can spread comments

      # over multiple lines in Python

  • Docstrings

String literals that follow a function’s definition are known as docstrings. They are useful for linking documentation to classes, functions, modules, and methods, but they can also be used to comment out code. String literals that are not assigned to a variable are ignored by Python.

  • E.g. ”’  This is a comment using string literals with triple quotes. ’’’
  • “””This is a multiline

comment using string literals

with triple quotes.  “””

Advantages of Using Comments:

  • Makes the code easily understandable by other programmers
  • The code becomes self-explanatory
  • It helps us remember why we used a specific command, method, or function in the code
  • It enables the interpreter to ignore some parts of the code while testing

 

Python Commenting Best Practices:

Comments are an important part of any program. As a result, learning how to write good comments is essential. Here are some qualities of good comments.

  • Keep them brief.
  • If you can’t write a clear comment, there may be a problem with the code.
  • Good comments do not excuse unclear code.
  • Good comments are self-explanatory.
  • Comments should not duplicate the code.
  • Describe idiomatic code in the comments.
  • Provide links to the original source of the copied code.
  • Include external links where they will be most useful.
  • Add comments when fixing bugs.
  • Mark incomplete implementations with comments.

 

Conclusion:

Writing good comments in Python will make it easier for other programmers to read and understand your code. It is one of the many basic concepts in Python that you must learn to understand the programming language. It is important to research and practice writing effective comments. I hope the above examples have shown that comments don’t excuse or fix bad code.

Visited 1 times, 1 visit(s) today