×

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

Subprocess Module in Python

What is a Process? Every program will have its respective system state, i.e., the state in which it is running. A program that is in a running state or in a...

7 minutes read.

Reason for Python So Popular

One of the languages that is witnessing outstanding development and acceptance every year in Python. Python has emerged as the programming language with the greatest rate of growth, and Stack...

3 minutes read.

Python Compiler

What is Compiler? The compiler is mainly a program used to convert the source code into the machine or binary code. The source code is generally a computer program written using...

6 minutes read.

Add Element to Tuple in Python

Python: Popular high-level, all-purpose programming language Python. The new version of the Python programming language, Python 3, is used for all software applications, including web development. Python is the best programming...

4 minutes read.

How to append an Array in Python

A group of objects kept at adjacent memory regions is known as an array. It is a container with a set capacity for a certain number of things, all of...

7 minutes read.

Python Sending Email

Python Sending Email Simple Mail Transfer Protocol (SMTP) is used to handle sending e-mail and routing e-mail between mail servers. When we send an email either form a web-application or from a local software...

3 minutes read.

Difference between For Loop and While Loop in Python

What is the “For” loop? The “For” Loop is a commonly used control structure in programming that allows us to repeat a set of actions multiple times. The “For” loop is...

3 minutes read.

Python lock

Before understanding the concept of Python-lock we need to understand what kind if condition we require to implement the locks in Python. Let us start with multithreading and its implementation...

5 minutes read.

Joint Plot in Python

Introduction to Joint plots: The joint plot is the finest approach to evaluate both the specific distribution of each variable and also the connection between the two variables. Three different plots make...

4 minutes read.

Flutter Python

The flutter is a frame work available in the python programming language, to create web applications, mobile apps. The flutter is generally used for the development of the backend of...

3 minutes read.

Import py file in Python

In Python programming language, a module is a single layer of block of Python code that can be loaded and used by importing into other Python block of code. A module...

4 minutes read.

Python set()

Python set() Class The set() class in Python returns a new set object, optionally with elements taken from iterable.  Syntax class set([iterable]) Parameter iterable : This parameter represents a sequence, collection or an iterator object Return This function returns a...

1 minute read.

Python List append() Method

Python List append() Method The list.append() method in Python adds or appends an item to the end of the list. Syntax list.append(x) Parameter x: It is a required parameter that represents an element of any type (string, number,...

1 minute read.

GUI Calculator in Python

Introduction In Python, we can develop a GUI(Graphical User Interface) with multiple options. It offers us some great and commonly used methods for the development of the Graphical User Interface. Tkinter...

4 minutes read.

How to run Python program in CMD

How to run python program in cmd Command Prompt provides us all together with a different approach for dealing with our programs. The programs can be executed by accessing the directories. In...

3 minutes read.

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.

Python If-else statement

In real life, there are situations where we have to make decisions for a particular circumstance and based on those decisions, and we plan our next move. The same thing...

4 minutes read.

Standard GUI Unit Converter using PyQt5 in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

Python Network Programming

In network programming, python plays a very important role. Python provides full support for encoding and decoding data and network protocol in its standard library. Writing a network program in...

3 minutes read.

Length of Tuple in Python

What is Tuple? Python is a data structure in a python programming language; it is the collection of the objects in a sequence. The tuples are immutable; that is, we cannot...

3 minutes read.