Course
Comments
Python Tutorial
This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.
Comments
What are Comments in Python?
A comment in a computer program is a piece of text that is meant to be an explanatory or descriptive annotation in the source code and is not to be considered by compiler/interpreter while generating machine language code. Ample use of comments in the source program makes it easier for everybody concerned to understand more about syntax, usage and logic of the algorithm etc. provided it has been commented nicely.
Python comments
make code easier to understand and they are completely ignored by the interpreter, which means you can provide as many comments as you like in your program to make it more readable and explainatory.
Python supports two types of comments:
Single-line
comments
Multi-line
comments
Single Line Comments in Python
In a Python script, the symbol
#
marks the beginning of comment line. It is effective till the end of line in the editor. If #
is the first character of line, then entire line will be assumed as a comment and interpreter will ignore it.# This is a commentprint ("Hello World")
If
#
is used in the middle of a line, text before it is a valid Python expression, while text following it is treated as comment.print ("How are you?") # This is also a comment but after a statement.
Sometime there is a situation when you don't want a particular line of Python code to be executed, then you can simply comment it as below:
# This is a comment# print ("How are you?")print ("Hello World")
Multi-line Comment in Python
In Python, there is no provision to write multi-line comment, or a block comment. (As in C/C++, where multiple lines inside /* .. */ are treated as multi-line comment).
Each line should have the "#" symbol at the start to be marked as comment in Python and that's how you can create multi-line comments in Python. Many Python IDEs have shortcuts to mark a block of statements as comment.
## This is a multi-line comment# which can span through multi-line.#print ("Hello World")
A triple quoted
multi-line
string is also treated as comment if it is not a docstring of a function or class.'''First line in the commentSecond line in the commentThird line in the comment'''print ("Hello World")
Practice with Online Editor
Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
Remember to save code(Ctrl
+S
Or
Command
+S
) before run it.