Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
Comments are an essential part of programming, as they allow you to add human-readable explanations and notes to your code. In Python, there are two types of comments: single-line comments and multi-line comments.
1. Single-line comments:
Single-line comments in Python begin with a hash symbol #
. Everything following the #
on the same line is considered a comment and ignored by the Python interpreter.
Here's an example of single-line comments:
# This is a single-line comment print("Hello, World!") # This is an inline comment
In this example, the first line is a single-line comment, and the second line contains an inline comment following the print
statement. Both comments are ignored by the Python interpreter and have no impact on the code execution.
2. Multi-line comments:
Python doesn't have a specific syntax for multi-line comments. However, you can use triple quotes ('''
or """
) to create a multi-line string, and if you don't assign it to a variable, it will be treated as a comment by the interpreter. This is a common convention for creating multi-line comments in Python.
Here's an example of a multi-line comment using triple quotes:
''' This is a multi-line comment. You can write explanations, notes, or any other text across multiple lines in your code. ''' print("Hello, World!")
In this example, the triple-quoted text is a multi-line comment. Although it's technically a string, the Python interpreter ignores it since it's not assigned to a variable or used in any expressions.
Best practices for comments:
In summary, comments are an essential part of programming in Python, helping you and others understand your code. Use single-line comments with the #
symbol and multi-line comments with triple quotes to document your code effectively.
How to write comments in Python:
# This is a single-line comment in Python print("Hello, World!") # This is an inline comment
Single-line comments in Python:
#
symbol and extend to the end of the line. They are used for short explanations or annotations.# This is a single-line comment print("Hello, World!")
Multi-line comments in Python:
'''
or """
) to create multi-line string literals, which are often used as multi-line comments.''' This is a multi-line comment ''' print("Hello, World!")
Commenting code in Python:
# Calculate the sum of two numbers num1 = 5 num2 = 7 result = num1 + num2 print("Sum:", result)
Comment conventions in Python:
# Good: Calculate and print the sum result = num1 + num2 print("Sum:", result) # Avoid: Adding numbers result = num1 + num2 # Sum print(result) # Print result
Ignoring code with comments in Python:
# This code is temporarily disabled # print("Hello, World!")
Python docstrings vs comments:
help()
. Comments are for in-code explanations.def add_numbers(a, b): """ Add two numbers. Parameters: a (int): The first number. b (int): The second number. Returns: int: The sum of the two numbers. """ return a + b
Removing comments in Python:
# This comment will be removed in the final code print("Hello, World!")