×

Python Comments

In this tutorial, we will discuss the importance of comments in the Python code and how various types of comments can be inserted in the code.

Comments are used to describe our code in simple words. They make our code more effective or easy to understand. The compiler will neglect the comments when the program is executed. The comments will be declared with a specific symbol when the compiler sees it, and then it will neglect the line. A Python comment can be placed anywhere in the program. But it must start with the symbol ‘#’. Otherwise, the Python compiler cannot understand it, and it will raise the error.

Single-Lined Comments:

  • In C and Java, the single-lined comments are declared with a double slash symbol (“//”).

Syntax:

//this is a comment.
  • And in Python, the singled-lined comments are given with the symbol hash(“#”).

Syntax:

#this is a comment.

Creating a Comment:

In Python, the comments always start with the symbol ‘#’.

Example:

#program to print Hello world
print(“Hello world”)

Output:

Hello world

we can also use comments in the program like


#simple program for singled-lined comments in python
A = 10      #declaring a variable A and assigned a value 10
B = 5         # declaring a variable B and assigned a value 5
C = A+B      #adding both the values and storing the result in the C variable
print(“The sum of A and B is”, C)    #printing the sum of two numbers A and B

Output:

The sum of A and B is 15.

Multi-Lined Comment:

In C, multi-lined comments are declared with the symbols ’/*’ at the beginning of the comment, and after the completion of the comment section, we must end with the symbol ‘*/’.

Syntax:

/*
Comment 1
Comment 2
Comment 3
.
.
.
.
.
Comment n
*/

Example:

#include <stdio.h>
#include <conio.h>
/*
Simple 
Program for 
Multi-lined comments in C
*/
Int main()
{
int a, b;   //variable declaration
clrscr();   //clear screen command
printf(“ Enter the value of a:”);
scanf(“%d”, &a);     //scanning a value
printf(“ Enter the value of b:”);
scanf(“%d”, &b);     //scanning b value
printf(“The sum of two numbers is %d”, (a+b));        //printing the sum of two numbers
return 0;
}

Output:

Enter the value of a:20
Enter the value of b:10
The Sum of two numbers is 30

In Python, multi-lined comments are declared with the symbols ‘#’ at the beginning of every line.

Syntax:

#comment 1
#comment 2
#comment 3
#comment 4
.
.
.
#comment n

Example:

#simple 
#program for multi-lined
#comment
A = 6 #A variable declared
B = 2                            #B variable declared
C = A/B
D = A%B
Print(“Dividing two numbers the output is”, c)
Print(“The modulus of two numbers is”, D)

Output:

Dividing two numbers, the output is 3
The modulus of two numbers is 0

We can also declare multi-lined comments in Python with the symbol triple quotes( “”” ) at the beginning of the comment, and after the completion of the comment section, we must give the same symbol( “”” ) at the end.

Syntax:

“””
Comment 1
Comment 2
Comment 3
.
.
.
.
.
Comment n
“””

Example:

“””
Here we are
Writing simple program
In Python
“””
X = 6 #declaring a variable x with the value 6
Y = 43                       #declaring a variable y with the value 43
if(x>y)                       #comparing x and y
    Print(“The largest value is “,x)     #printing x value if it is larger than y
else
    Print(“The largest value is “,y)      #printing y value if it is larger than x

Output:

The largest value is 43

Explanation:

We have declared two variables, x and y, with some values in the program. We assigned 6 to x variable and 43 to y variable. After that, we have given the if condition, which checks whether x is greater than y or not. If x is greater than y, then the x value will be printed. Otherwise, the y value will be printed.


Related Topics

Map Syntax in Python

Introduction: In Python, a function called map acts as an iterator, returning a result after each item in an iterable has been subjected to a function (tuple, lists, etc.). When you...

6 minutes read.

Calculator Program in Python

Simple Calculator Program in Python This example helps to learn how to create a simple calculator program to perform operations like addition, subtraction, multiplication, and division. Source Code The following is the source...

2 minutes read.

Standard Scalar in Python

Python is a vast and open-source language that contains many libraries, modules, and functions. These functions in python are reusable codes where we don’t need to type the whole code...

4 minutes read.

How to change a value of a tuple in Python

Python is the language for newly evolving technologies and has become one of the most popular programming languages in the world. It is used in everything right, from simple algorithm...

3 minutes read.

Applications of Python

Top 10 Applications of Python in 2020- 2021 Python language is famous for its general-purpose nature, which enables developers to use it in every field of development. Python can be found...

6 minutes read.

How to Download all Modules in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Write Dictionary to CSV in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

How to append a string in python

Adding or appending a string to another string is easy in Python. It is called concatenation and is a basic concept of strings in almost all programming languages. Ways to Append...

4 minutes read.

Python Set issuperset() method

Python Set issuperset() method The set.issuperset() method in Python returns a boolean value True if all items in the specified set exists in the original set, else it returns False. Syntax set.issuperset (set1) Parameter set- This parameter represents the...

2 minutes read.

How to Compare two Lists in Python?

How to Compare two Lists in Python The list is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets [...

4 minutes read.

List Comprehension in Python3

List A list in python is a complex data type, and a list is a collection of the number of data. The data variable may or may not be of the...

5 minutes read.

Is Python Case Sensitive

Case sensitivity is the mode of dealing with the written alphabet. The cases of the alphabet are examined and based on these words are being treated. The uppercase and lowercase...

3 minutes read.

Python Program to Find the Greatest Among Three Numbers

Python Program to find the greatest among three numbers We have many approaches to get the largest of three numbers, and here we will discuss a few of them. This python...

2 minutes read.

Python hash() function

Python hash() function The hash() function in Python returns the hash value of the object (if it has one).  Syntax hash(object) Parameter obj : This parameter represents the object which we need to convert into hash. Return This...

1 minute read.

Python String swapcase() method

Python String swapcase() method The string.swapcase() method in Python returns a copy of the string with uppercase characters converted to lowercase and vice versa. Syntax string.swapcase() Parameter NA Return This method returns a copy of the string...

1 minute read.

Python List sort() method

The list.sort () method in Python sorts the items of the list in place. Syntax list.sort(key=None, reverse=False) Parameter reverse: If a Boolean value ‘True’ is passed, the  sorting will be done in the descending order else for ‘False’...

2 minutes read.

Get index of element in array in python

Index : Index is a number where the element is located in the given list. In other words it is the position of the element in the list. Index can also...

4 minutes read.

Python Dictionary clear() method

Python Dictionary clear() method The dictionary.clear() method in Python removes all the elements from a dictionary. Syntax dictionary.clear() Parameter NA Return None Example 1 # Python program explaining # the dictionary.clear() method # initialising the dictionary fruits =...

1 minute read.

Python String expandtabs() method

Python String expandtabs() method The string.expandtabs() method in Python returns a copy of the string where all tab characters are expanded using spaces. Syntax String.expandtabs([tabsize]) Parameter tabsize: This parameter specifies the number of characters to...

1 minute read.

How to Install PIP In Python

How to Install PIP In Python The libraries for Python have made our work easier than we expected. From a simple addition of two numbers to applying algorithms on the big...

4 minutes read.