Python Comments Notes

Objectives: Python Comments Notes

Python Comments Notes

Python Comments

Comments are a way to explain code, make it readable, and help developers understand it. Python ignores comments when running the program.

Purpose of Comments

  • Explain what the code does for humans reading it.
  • Improve readability of your code.
  • Temporarily disable code while testing.

How the Python Compiler Treats Comments

When you write a Python program, the Python interpreter reads your code line by line. If it sees a comment (anything starting with #), it ignores it completely. It does not generate any bytecode for comments. This means comments have zero effect on the execution of your program.

Single-Line Comments

Single-line comments start with a # symbol. Anything after # on the same line is ignored by Python.

Example 1: Basic Comment

# This is a comment
print("Hello, World!")  # This prints a message

Explanation: The first line is ignored by the Python interpreter. The second line prints text, while the comment after # is also ignored.

Example 2: Preventing Code Execution

# print("This will not run")
print("Cheers, Mate!")

Explanation: The first line is commented out, so Python skips it. Only "Cheers, Mate!" is printed.

Multiline Comments

Python does not have a special syntax for multiline comments, but there are two ways to write them:

Method 1: Using Multiple # Lines

# This is a comment
# written in
# more than one line
print("Hello, World!")

Explanation: Each line starts with #. Python ignores all of them and executes the print statement.

Method 2: Using Triple-Quoted Strings (Docstring Style)

You can use """ or ''' to write text that spans multiple lines. If you do not assign it to a variable, Python ignores it. This is sometimes called a "docstring comment."

"""
This is a multiline comment
using triple quotes.
Python ignores it if it's not assigned to a variable.
"""
print("Hello, World!")

Explanation: The triple-quoted string is ignored by the interpreter, so the print statement runs normally.

Behind the Scenes: How Python Handles Comments

  1. You write Python code in a file with .py extension.
  2. Python reads the file line by line using the interpreter.
  3. When it encounters # or an ignored string, it skips it without converting it to bytecode.
  4. All executable code is compiled into bytecode, which is then executed by the Python virtual machine (PVM).
  5. Comments have no runtime effect and are purely for humans.

Real-World Usage Examples

Example 1: Explaining a Complex Calculation

# Calculate area of a circle
radius = 5
pi = 3.14159
area = pi * radius**2  # formula: πr²
print("Area:", area)

Explanation: The comment explains the formula. Anyone reading the code immediately understands what the calculation does.

Example 2: Disabling Code for Testing

Explanation: The debug print is ignored. This technique helps developers test code without deleting lines.

Best Practices

  • Write meaningful comments that explain “why” rather than “what” (code itself often shows what).
  • Keep comments up to date; outdated comments confuse developers.
  • Use comments to explain tricky logic, not obvious statements.
  • Use docstrings for functions and classes to describe their purpose.

Summary

Comments in Python are a tool for developers, completely ignored by the Python compiler. They can be single-line, multiline with #, or triple-quoted strings. Using comments wisely makes code more readable, maintainable, and easier for collaboration.

Reference Book: N/A

Author name: MWALA_LEARN Work email: biasharabora12@gmail.com
#MWALA_LEARN Powered by MwalaJS #https://mwalajs.biasharabora.com
#https://educenter.biasharabora.com

:: 1::