Which symbol is used to start a comment in Python?
Analysis & Theory
In Python, the hash symbol (#) is used to begin a comment.
What is the purpose of comments in Python?
C
To describe code or disable parts of it
Analysis & Theory
Comments are used to explain code and to temporarily disable code without deleting it.
Which of the following is a valid single-line comment?
C
/* This is a comment */
D
<!-- This is a comment -->
Analysis & Theory
Python uses `#` for single-line comments.
How do you write a multi-line comment in Python?
A
# This is
a multi-line
comment
C
''' This is
a multi-line comment '''
D
<!-- Multiple lines -->
Analysis & Theory
Triple quotes (`'''` or `"""`) are commonly used for multi-line comments, although technically they are multi-line strings.
Which of the following is treated as a comment by Python?
A
`Comment inside backticks`
B
# This is ignored by the interpreter
C
print('This is a comment')
D
// Not a Python comment
Analysis & Theory
Lines starting with `#` are ignored by the Python interpreter.
Can Python comments be placed at the end of a line of code?
Analysis & Theory
You can use `#` at the end of a line of code to add a comment.
What will the following code do?
```python
# print("Hello")
print("World")
```
A
Print both 'Hello' and 'World'
Analysis & Theory
The first line is a comment and ignored. Only 'World' is printed.
Which of the following is NOT a correct way to use comments?
C
""" This is a docstring """
D
comment This is a comment
Analysis & Theory
`comment` is not a valid keyword in Python. Use `#` or triple quotes for comments/docstrings.
What is a docstring in Python?
A
A comment before a variable
B
A special kind of string used to document a function or class
Analysis & Theory
Docstrings are string literals used to document functions, classes, or modules.
How do you write a docstring for a function in Python?
A
# This function adds numbers
C
'''This function adds numbers'''
Analysis & Theory
Use triple quotes (`'''` or `"""`) to write docstrings inside functions or classes.