what are comments in Python language? How do you write a comments in Python?
Comments are description about line or block about code.
Generally Comments are for programmers not for Python interpreter. At runtime, These are ignored by Python.
In Python, You can write a comments in different following ways
- Single line comments
- Multi Line comments
Comments can be added as single new line or inline or multiple lines of code It is always good practise to add Comments to the below blocks or lines of code
- Classes description
- before method declaration
- Any complex logic code
- documentation comments
Python single line comments
Comments are placed in a new line or inline.
- It always starts with pound (#) symbol character and ends with line break.
- an string of text begins with # symbol are ignored by Python compiler.
- It is an description or piece of text for single line of code.
- These comments are not required at starts in begin of line, but also can write in middle or end of line Syntax:
\#This is single line comments in Python Code
Example
hello.py
\#Hello world python example first app
prints ("Hello world program")
prints ("Thanks") #can also written here also
Learned single line comments, How do you write a multi line comments
Multi line comments in Python
Comments written in multiple also code block comments.
Python does not support for multi line comments in pythons.
You can add single line comment syntax to each line as given below
Here is a way we can write a multi line comments with #\ symbol. Each line starts with #\ character.
#\ multi line comments 1
#\ multi line comments 2
#\ multi line comments 3
Block level comments in Python Example:
\#Hello world sample program
\#Basic program to print hello world to console
\#First code to write in Python language
print("Hello world sample program")
An another way, python ignores text placed inside a triple quotes.
Python provides special syntax to write a block level comments
"""
This comment is a multi line comment 1
This comment is a multi line comment 2
This comment is a multi line comment 3
"""
This allows you to write a multiple lines of code and ignored by compiler.
- This always starts with
=begin
and ends with=end
string span in multiple lines - There is space between = and begin or = and end
Block level comments in Ruby Example:
\"""
Hello world sample program
Basic program to print hello world to console
First code to write in Ruby language
\"""
print("Hello world sample program")
Python documentation comments
documentation comments adds for API documentation for developer.
Will write more about this in later.