×

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

SKLearn Linear Module

The SK learn linear module is one such module that helps to study the relationship between the independent and dependent variables.The linear module can be implemented by using the best...

3 minutes read.

How to create a DataFrames in Python

How to create a DataFrames in Python Data Frame is a data structure in which data is stored in tabular form. They can also be referred as two-dimensional collection of data. Various...

5 minutes read.

Python pycountry

What is Pycountry? Python programming language: 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...

4 minutes read.

How to get current date in python?

How to get current date in python? In Python programming language, date and time are not just a data form, but it is possible to import a method called datetime to operate...

3 minutes read.

Python program to find factorial of a given number

Python program to find factorial of a given number Factorial is a non-negative integer. It is the product of all positive integers from 1 to the number we are going to...

3 minutes read.

Python len() function

Python len() function The len() function in Python  returns the number of items in an object. Syntax len(s) Parameter s: This parameter represents a sequence (such as a string, bytes, tuple, list, or range) or...

1 minute read.

Python Ways to find nth occurrence of substring in a string

Introduction In Python, a string is a collection of characters that can be employed to conduct additional operations. In Python, a substring is a group of characters that are a part...

4 minutes read.

Type casting in Python

Introduction Type Casting is defined as the method of converting the variables/values data type into a certain data type for matching the operation needed to be performed by the users. This...

4 minutes read.

Number pattern in Python

Number pattern in Python: This article explains how to print number patterns in Python. The FOR loop, while loop, and range() functions are used in the following Python programs to...

4 minutes read.

Python String decode() method

Python String decode() method The string.decode() method decodes the string using the codec registered for encoding. Default encoding is the current default string encoding. It may raise errors to set a different error handling...

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

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

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.

Python program to convert Celsius into Fahrenheit

Python program to convert Celsius into Fahrenheit This program explains how we can take the temperature in Celsius and convert them into Fahrenheit. Celsius Celsius, also known as centigrade, is a measurement unit...

1 minute read.

Python Subprocess Call Example

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

4 minutes read.

An Introduction to Subprocess in Python with Examples

Subprocess in Python By starting new processes, the Python function subprocess is utilized to run extra programs and applications. It makes it possible to run brand-new apps straight from a Programming...

6 minutes read.

Python MySQL Update Operation

Python MySQL Update Operation: In this part of tutorial, we will learn that how can we update a table present in SQL database through our Python program. As like SQL,...

4 minutes read.

Managing Multiple Python Versions With pyenv

Introduction If you had ever wished to assist to an application that makes use of several different Python versions but weren't sure whether you would quickly test them all? Do you...

4 minutes read.

Conditional Statements in python

In this article, we will learn about conditional statements and how to apply conditional statements in Python. A decision-making statement is another name for a conditional statement. Decision-making is an important...

5 minutes read.