Comments in Python #
In Python, comments are used to add explanatory notes or remarks to the code, without affecting the actual execution of the program. The interpreter ignores comments while executing the program. Comments are a useful tool for documenting code, providing context, and making it easier for others to understand your code.
Python has two ways to create comments:
Single-line comments #
Single-line comments start with a hash symbol # and continue until the end of the line. Anything after the hash symbol on that line is ignored by the interpreter.
Example:
# This is a single-line comment in Python
print("Hello World!") # This line will print "Hello World!"
Multi-line comments #
Multi-line comments are created by enclosing a block of text in triple quotes """, also known as docstrings. These can be used for documenting classes, functions, and modules.
Example:
"""
This is a multi-line comment or docstring in Python.
It can span across multiple lines and can be used to
document classes, functions, and modules.
"""
def greet(name):
"""
This function greets the person passed in as a parameter.
"""
print("Hello, " + name + ". How are you?")
Exercise #
Write comments in Python
Tests #
- Write a single-line comment with at least 5 characters.
- Write a multi-line comment with at least 5 characters.
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules