YouTip LogoYouTip

Python3 Comment

# Python3.x Python3 Comments In Python, comments do not affect the execution of the program, but they make the code easier to read and understand. Comments in Python include **single-line comments** and **multi-line comments**. * * * ## Single-line Comments In Python, single-line comments start with a `#`. All text following the `#` symbol is considered a comment and will not be executed by the interpreter. ## Example # This is a comment print("Hello, World!") # This is also a comment * * * ## Multi-line Comments In Python, multi-line strings (text blocks enclosed by three single quotes `'''` or three double quotes `"""`) can be used as multi-line comments. ### 1. Using Three Single Quotes ## Example #!/usr/bin/python3 ''' This is a multi-line comment, using three single quotes This is a multi-line comment, using three single quotes This is a multi-line comment, using three single quotes ''' print("Hello, World!") ### 2. Using Three Double Quotes ## Example #!/usr/bin/python3 """ This is a multi-line comment, using three double quotes This is a multi-line comment, using three double quotes This is a multi-line comment, using three double quotes """ print("Hello, World!") > **Note**: Although multi-line strings are used here as multi-line comments, they are actually strings. As long as we don't use them, they won't affect the program's execution. > > > These strings can be placed in certain positions in the code without causing actual execution, thereby achieving the effect of a comment. ### Multi-line Comment Considerations In Python, multi-line comments are defined by three single quotes `'''` or three double quotes `"""`. **This comment style cannot be nested.** When you start a multi-line comment block, Python will treat all subsequent lines as comments until it encounters another set of three single quotes or three double quotes. **Nesting multi-line comments will cause a syntax error:** ## Example (Incorrect) ''' This is an outer multi-line comment Can contain some descriptive content ''' This is an attempted nested multi-line comment Will cause a syntax error ''' ''' In this example, the inner three single quotes are not correctly recognized as the end of a multi-line comment but are interpreted as ordinary strings, which will lead to incorrect code structure. **Correct approach: Using single-line comments for nesting** ## Example (Correct) ''' This is an outer multi-line comment Can contain some descriptive content # This is an inner single-line comment # Can be nested within a multi-line comment ''' * * * ## Docstring Python's Docstring (Documentation String) is a special type of comment used to add documentation to functions, classes, modules, etc. It is similar to Java's Javadoc but more powerful and flexible. Unlike ordinary comments, **Docstrings can be accessed directly via the `__doc__` attribute** and can also be viewed using the `help()` function. ### Basic Syntax A Docstring is enclosed by three double quotes `"""` or three single quotes `'''` and placed at the beginning of a function, class, or module. ## Example def add(a, b): """Returns the sum of two numbers""" return a + b # Access via the __doc__ attribute print(add.__doc__)# Output: Returns the sum of two numbers ### Using help() to View Documentation ## Example def add(a, b): """Returns the sum of two numbers""" return a + b # Using the help() function help(add) **Expected Output:** Help on function add in module __main__: add(a, b) Returns the sum of two numbers ### Extracting Documentation with the inspect Module Python's standard library provides the `inspect` module, which can directly extract documentation content: ## Example import inspect def add(a, b): """Returns the sum of two numbers""" return a + b # Using inspect.getdoc() to get the documentation print(inspect.getdoc(add))# Output: Returns the sum of two numbers **Expected Output:** Returns the sum of two numbers ### Multi-line Docstrings For complex functions, you can use multi-line Docstrings: ## Example def calculate(a, b, operation="add"): """ Performs a mathematical operation. Parameters: a: The first number b: The second number operation: The operation type, optional "add", "subtract", "multiply" Returns: The result of the calculation """ if operation =="add": return a + b elif operation =="subtract": return a - b elif operation =="multiply": return a * b else: raise ValueError("Unsupported operation")
← Python3 NumberPython3 Interpreter β†’