Comments in Python
In general, Comments are used in a programming language to describe the program or to hide the some part of code from the interpreter.
Comments in Python can be used to explain any program code. It can also be used to hide the code as well.
Comment is not a part of the program, but it enhances the interactivity of the program and makes the program readable.
Python supports two types of comments:
- Single Line Comments
- Multi Line Comments
1. Single Line Comments in Python:
In case user wants to specify a single line comment, then comment must start with ‘#’
format:
# This is single line commentExample: "scomment.py"
# This is single line comment.
print("Hello Python")
Output: $python3 scomment.py
Hello Python2. Multi Line Comments in Python
Multi lined comment can be given inside triple quotes.The must start at begining of the line.
format:
''' This
Is
Multiline comment'''Example: "mcomment.py"
'''This
is
Multi line comment'''
print("Hello Python")
Output: $python3 mcomment.py
Hello Python⛳ Python Example Program: Demonstrates usage of Comments in Python
Example: "comments.py"
# This example demonstrates usage of Comments
''' print() used to
print/display
text on screen '''
print("Welcome to Python")
#Assign value to variables
a=20
b=30
#print sum of two numbers
print("Sum is")
print(a+b)
Output: $python3 comments.py
Welcome to Python
Sum is
50