What are the comments?
- Comments are a way to provide an explanation or clarification for the code.
- Comments should be added to the code to explain its functionality to others.
In Python, there are two types of comments.
1 . Single-Line comments:
- To add a single-line comment in Python, you need to begin the line with the '#' symbol.
- Use for a brief explanation
Input:
x = 5 # Assigns the value 5 to x
print(x)
Output:
5
2 . Multi-line comments (DocString):
- For more extensive explanation or documentation, multiline comments are used called DocString
- These strings are enclosed in triple cotes ( ' ' ' or '" " ")
Input:
def greet(name):
"""Prints a personalized greeting.
:param name: The name of the person to greet"""
print(f"Hello, {name}!")
greet("Alice")
Output:
Hello, Alice!
- You can also use ' # ' in each line for Multi-line comments
Input:
# This is a comment
# written in
# more than just one line
print("Hello, World!")
Output:
Hello, World!
Previous(Python Indentation)
Next(Python Variable)
Comments
Post a Comment