Python Comments

PyinMath
0

 Lines of code in Python that are ignored by the interpreter while the programme is running are known as comments. Comments improve the readability of the code and aid programmers in thoroughly comprehending it.

Uses of  Comments:

Python uses comments, which are brief explanations that are added to the code to make it easier to read. They are used by developers to record their own thought processes as they write code. It clarifies the fundamental reasoning behind why a certain line of code was created. They are only intended to help programmers or other developers understand a particular section of code.

Comments are used for:
  • Explaining the code to everyone 
  • Increasing readability
  • Make the code easier to understand 

Types of Comments in Python:

There are two types of comments in python:
  1. Single-line comment 
  2. Multi-line comment

Single-line Comment in Python:

A comment that is  a single line begins and ends on that same line. If the comment exceeds more than one line, add a hashtag to the following line before continuing. Short explanations of variables, function declarations, and expressions can be provided using Python's single-line comments. To create a single-line comment, we use the  #  symbol. Code after (#) are ignored by the interpreter.

Example
# create some variables
name='PyinMath' # name is a string with single quotation
another_name="Physicsvilla" # another_name is a string with double quotation
no=3000 # no is a number 
print(name)
print(another_name)
print(no) 
Output:

Here, we created four single-line comments:
  1. # create some variable
  2. # name is a string with single quotation
  3. # another_name is a string with double quotation
  4. # no is a number

Single-line comment using string:

We can also creat single line comment using  quotation.

Example
'This is a single line comment using quotation'
print('single line')

Multi-line Comment in Python:

Multi-line comments are not supported by Python. There are numerous solutions to this problem, though. Although none of these approaches are technically multi-line comments, you can still use them as such.
There are two ways through which we can write multi-line comments.
  • Using multiple hashtags
  • Using triple quotes 

Multiline comments using multiple hashtags:

In python, we can write multiline comments using multiple hashtags at the beginning of every line of the comment.


Example
# this is a
# multiline comment
# using multiple hashtags
print('multiline comment using hashtag')
Output:

Multiline comments using string literals:

Python ignores the string literals that are not assigned as variable or function.


Example
'''This is a multiline 
comment using 
triple quotes'''
print('hello world')
Output:


Tags

Post a Comment

0Comments
Post a Comment (0)