×

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

Confusion Matrix Visualization Python

The confusion matrix is a two-dimensional array that compares the anticipated and actual category labels. These are the True Positive, True Negative, False Positive, and False Negative classification categories for...

4 minutes read.

Speech Recognition in Python

What is Speech Recognition? Speech Recognition is a term defined for automatic recognition of human speech. Speech recognition is the most significant activity in the domain of the interaction between the...

8 minutes read.

Exit program in python

Exit program in python The quit(), exit(), and sys.exit() functions have almost the same functionality as they raise the SystemExit exception by which the Python interpreter exit and no stack traceback...

2 minutes read.

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered. Following...

3 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.

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 Linear regression

In this tutorial, we will understand the meaning, usage, and types of Linear Regression. Further, we will comprehend the terms cost function and Optimization. Linear Regression is the widely used Machine...

3 minutes read.

Python Break Statement

In Python, loops are used to automate and repeat processes in an effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration,...

2 minutes read.

Python Keywords

If you are trying to learn a programming language, you need to have a basic idea of "What are keywords" and "How they are used". You can learn about keywords in...

7 minutes read.

Python String isalnum() method

Python String isalnum() method The string.isalnum () method in Python returns a boolean value true if all characters in the string are alphanumeric else for any other value it returns false. Syntax String.isalnum() Parameter NA Return This...

1 minute read.

How to reverse a string in python

How to reverse a string in python A Brief About Strings- The String is a data type in Python that has a sequence of characters. This series of characters are represented in...

4 minutes read.

Python id() function

Python id() function The id() function in Python returns an id for the specified object where all the objects in has its own unique id. Syntax id(object) Parameter object: This parameter represents any object, String, Number, List,...

1 minute read.

Why learn Python?

All things considered, learning python would be extraordinary, it'll acquaint you with the universe of dynamic programming dialects, if you're somebody who has had a semester of involvement with C. Python...

4 minutes read.

Python vs Java

There are a lot of programming languages out there, and every day, a new programming language is developing in some parts of the world. Each programming language is a mixture...

5 minutes read.

GUI Calendar using PyQt5 in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

Python Static Method

Introduction to methods that are majorly used in Python: Class MethodsInstance MethodsStatic Methods Before going to the in-depth concept of Static Methods, let us briefly discuss Class Methods and Instance Methods. Class Methods The...

4 minutes read.

Python Empty Tuple

How to Create an Empty Tuple Tuple A tuple is a data structure used to store non-homogeneous data elements. These non-homogeneous data elements consist of integer data type, character data type, String...

3 minutes read.

Python JSON

In this tutorial, we will learn about JSON in the Python programming language. We will focus on its features, Guidelines to be kept in mind while writing its syntax, the...

3 minutes read.

Python Command line Arguments

Python Command line Arguments The command-line argument is used to the change functionality of the program. It's an extra command the programmer can use while launching a program. These commands have many uses...

7 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.