How To Write Comments in Python 3?
Comments are like sticky notes in code, explaining and reminding programmers about the code’s purpose. They’re not actual commands for the computer, just helpful notes for humans. When running a program, comments don’t show up in the output—they’re just for documentation of the code understanding.
Syntax:Â a comment begins with the hash symbol (#)
# This is a comment
Example:
# This represents the syntax for a comment.Â
print("Hello World")Â
# Comments have no impact on the interpreter or output.
Output :Â Â
Hello World
Keep comments aligned with the code they explain.
Types of Comments:
There are 4 types of Comments:
1. Single-Line Comments
2. Multi-line (Block) comments
3. Inline Style comments
4. Docstring comments
1. Single-Line Comments:
comments that begin with ‘#’ and include whitespace are known as single-line comments. They can only be written on a single line and are the only way to add comments in Python.
Example:
# This is the single-line comment.
2. Multi-line (Block) comments:
Unlike some programming languages, Python doesn’t have built-in support for multi-line comment blocks. However, you can use consecutive ‘#’ single-line comments to comment out multiple lines of code. Here are some examples of block comments:
Example:
# These comments can function asÂ
# both single-line and multi-line (block)Â
# comments in Python.
3. Inline Style comments:
Comments that are written on the same line as the code they explain are called inline comments. They usually look like this:
Example:
n = 3Â Â Â Â # This is an inline comment
z = x + y  # Add the value of 'x' and 'y' to 'z'
4. Docstring Comments:
Python docstrings, short for documentation strings, are a helpful way to add explanations to Python modules, functions, classes, and methods. Think of them like comments in the code, but instead of explaining how something works, docstrings focus on describing what a function does.
Example:
def example_function():
  """ Illustrates the use of docstrings and performs no significant operation."""
Â
  return None
 Â
 Â
print("Using __doc__:")Â
print(example_function.__doc__)Â
 Â
print("Using help:")Â
help(example_function)
Output:
Using __doc__:
Illustrates the use of docstrings and performs no significant operation.
Using help:
sh: 1: more: not found
Help on function example_function in module __main__:
example_function()
  Illustrates the use of docstrings and performs no significant operation.
Advantages and Uses of Comments:
Planning and Reviewing: Comments allow us to write pseudocode, a combination of natural language and high-level programming language, before actually writing the source code. Pseudocode makes it easier to review the code as it is more understandable than the program itself.
Example:
# This function adds two specified numbersÂ
def add_numbers(x, y):Â
 # Stores the sum of given numbers in 'result'.Â
 result = x + yÂ
 # Returns the calculated sum.Â
 return resultÂ
# Assigning values to 'x' and 'y' and calling the add_numbers() functionÂ
x_value = 20
y_value = 3
total_sum = add_numbers(x_value, y_value)Â
# Displaying the calculated sum from the functionÂ
print(total_sum)
Output:
23
Debugging:Â A common method for debugging is the brute force approach, where print statements are strategically inserted throughout the program to display intermediate values. The goal is to identify errors by examining these printed values. Once debugging is complete, comments are added to the document and explain the purpose of those print statements. Therefore, comments are also valuable for debugging purposes.
Example:
v = 12
if v == 12:
  print("True")
# elif value == 0:
 # print("False")
else:
  print("Debugging")
Output:
True
Conclusion:
We’ve reached the end of the ‘Comments in Python’ tutorial. Adding clear comments in your Python code is essential for making it readable to fellow programmers. Understanding this simple concept is crucial as you continue to learn Python and master the language