Lesson 4 of 4010%
(Progress persistence is disabled until Phase 9)
Python
Beginner
Comments
Learn how to write notes in your code that Python ignores.
Comments
What You’ll Learn
You’ll learn how to write comments in Python to explain your code and temporarily disable parts of it during testing.
Single-Line Comments
Comments start with a #, and Python will ignore the rest of the line.
# This is a comment
print("Hello, World!")
print("BrowserCode") # This is a comment at the end of a line
Multi-Line Comments
Python does not really have a syntax for multi-line comments. Instead, you can just insert a # for each line:
# This is a comment
# written in
# more than just one line
print("Hello, World!")
Alternatively, you can use a multiline string (triple quotes). Since Python ignores string literals that are not assigned to a variable, this acts as a comment:
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Why Use Comments?
- Explain code: Help others (and your future self) understand complex logic.
- Prevent execution: Quickly disable code when trying to find bugs.