Home » Python

Comments in Python

Python comments: In this tutorial, we will learn how to comments and different types of comments with examples in Python programming language?
Submitted by Bipin Kumar, on October 20, 2019

Python comments

Comments in Python are used to improve the readability of the code. It is useful information given by the programmer in source code for a better understanding of code and logic that they have used to solve the given problem to the reader. Comments are not executed during the compilation and also not show on the output.

In Python, comments are done by using the hash (#) and delimiters ("" or ''') symbol with no whitespace between starting. Now we will try to learn the types of comments with some examples.

Types of comments in Python

There are two types of comments in Python,

  1. Single line comment (#)
  2. Multi-line string as comment (''')

1) Single line comments

In Python, single-line comments are used for comments one-line statements like explanations of different variables, functions, expressions, etc. To do single-line comments a hash (#) symbol is used with no whitespace when the comments go to the next line then must put one another hashtag(#) at the start of the next line. Let's see an example and try to understand how we apply the single-line comments in the program.

Example:

# Single line comments example
# a program to print a given string and addition.

print('Welcome @ IncludeHelp')
a=2
b=5
print(a+b)

# addition of both numbers by using plus(+) sign.

Output

Welcome @ IncludeHelp
7

2) Multi-line string comments

As we have seen in the above example that single-line comments, for multi-line we have to put a hash (#) symbol in each line. In Python, To overcome this problem multi-line string comments using delimiter (''') are provided. It is useful when does not fit in one line. For multiline string comments, we have to enclose the string with delimiter at both ends.

Note: A delimiter is a sequence of one or more characters.

Example:

'''
    Here we will check a given number n is even or odd 
    with multi-line comments in Python.
'''

n=6768

if n%2==0:
       print("Even number.")
else:
      print("Odd number.")

Output

Even number.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.