05 How to write comments in Python?
Like other programming languages Python also has commenting capability for the purpose of in-code documentation.
Single Line Comment:
Comments start with a #, and Python will render the rest of the line as a comment:
Example:
#This is a comment.
print("Hello, World!")
Or
print("Hello, World!") #This is a comment
Multy-Line Comment:
In-fact Python does not have any syntax for multi-line comments. To add a multiline comment you could insert a #
for each line:
Example:
#This is a comment
#written in
#more than just one line
print("Hello, World!")
Or, you can also use a multiline string for multi-line comment.
Python ignores string literals that are not assigned to a variable, you can add a multiline string (triple quotes) in your code, and place your comment inside it.
Example:
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
As long as the string is not assigned to a variable, Python will read the code, but then ignore it, and you have made a multiline comment.
- Information Technology
- Office Equipment and Supplies
- Cars and Trucks
- Persons
- Books and Authors
- Tutorials
- Art
- Causes
- Crafts
- Dance
- Drinks
- Film
- Fitness
- Food
- Games
- Gardening
- Health
- Home
- Literature
- Music
- Networking
- Other
- Party
- Religion
- Shopping
- Sports
- Theater
- Wellness