Python Comments
In Python, comments are lines in the code that are ignored by the interpreter. They are used to explain the code, making it more readable and understandable for others (or yourself) in the future. Comments can describe the purpose of a block of code or explain complex logic. They are particularly useful for documentation and debugging.
Python supports two types of comments:
1. Single-line comments:
A single-line comment starts with the # symbol. Anything after the # on that line will be treated as a comment and ignored during execution.
Example:
# This is a single-line comment
print(“Hello World!”) # This will print “Hello, World!” to the console
2. Multi-line comments (Docstrings):
Python does not have a specific syntax for multi-line comments like some other programming languages. However, you can use docstrings (multi-line string literals) for a similar purpose. These are typically used for documentation but can also serve as multi-line comments if they are not assigned to a variable or used for documentation.
Example:
”””
This is a multi line comment
It can span up to multiple lines
”””
Course Video
Course Video English
YouTube Reference :
Use a docstring with triple quotes inside the class.
Use triple quotes (”’ or “””) for multi-line header comments.
Use triple quotes for multi-line header comments or a # for single-line comments to add descriptive text at the beginning of a code block.
Comments in Python are used to explain and document code. They are ignored by the Python interpreter and do not affect the execution of the program.
Comments cannot be printed as they are not executed by Python. You must convert them into regular strings to display them.
Use # at the start of each line or wrap the text in triple quotes (”’ or “””) for temporary multi-line commenting.
Type comments provide type hints for variables and functions, such as # type: int, and are useful for static type checking tools like mypy.
Use the # symbol for single-line comments. Everything following # on that line is ignored by the Python interpreter.
A single-line comment starts with the # symbol and extends to the end of the line, explaining a particular piece of code.
Use multiple lines with # at the start of each line, or use triple quotes (”’ or “””) to create a descriptive block for explanation or documentation.