×

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

Python List clear() method

Python List clear() method The list.clear () method in Python extends the list by appending all the items from the iterable. Syntax list.clear() Parameter NA Example 1 # Python Program explaining # the list.clear() method week_list = ["Monday", "Tuesday", "Wednesday","Thursday","Friday",...

1 minute read.

Merge Sort using Python

Merge Sort is a technique that is used for sorting elements in an array using a special method known as divide and conquer. It is the best example of the...

5 minutes read.

Nested for Loop in Python

"Loop" is one of the foundation concepts to learn programming in any language. Nested loops are significant steps in solving many types of problems, ranging from basic to complex scenarios....

9 minutes read.

Spotify API in Python

The application programming interface is referred to as API. In essence, an API serves as a layer of communication or, as the name suggests, an interface that enables systems to...

3 minutes read.

Python Trigonometric Functions

Before jumping right into the functions and how to use them we need to know what do trigonometric functions mean and how many functions does python has. What are Trigonometric functions? Trigonometric...

3 minutes read.

Python Dictionary popitem() method

Python Dictionary popitem() method The dictionary.popitem() method in Python removes the item that was last inserted into the dictionary. Syntax dictionary.popitem() Parameter NA Return This method returns an arbitrary element (key, value) pair from the given dictionary...

1 minute read.

Difference between Input() and raw_input() functions in Python

There are two input functions in Python that are used for taking input are given below: raw_input()input() What is raw_input() Function? raw_input() function is a built-in function in Python. It is used to...

6 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 Convert String to List In Python?

How to Convert String to List In Python? We all are familiar with what strings and lists are, let us have a quick revision on them- Strings are a sequence of characters...

4 minutes read.

Event Key in Python

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

3 minutes read.

Convert int to Float in Python using Pandas

Introduction We have already learnt about how to convert float variables to int. Now let us know how to convert int variables to float So let us create another Dataset on which...

2 minutes read.

Create the First GUI Application using PyQt5 in Python

GUI: A graphical user interface, or GUI, is present on most personal computers. It provides a simple experience for individuals with various computing skill levels. GUI apps may take more resources...

3 minutes read.

What is the Python Global Interpreter Lock?

Introduction When working with processes, Python employs a form of process lock called the Global Interpreter Lock (GIL). Python typically executes a collection of typed statements using just one thread. It...

4 minutes read.

Standard Scaler in SKLearn

The sci kit learns in python is a library thatch is used in machine learning which is used to work on data modeling.It is only focused on the data modeling,...

4 minutes read.

Python Marshmallow

Python:  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 is said...

3 minutes read.

App Config Python

An XML file called App. Config serves as the document for any programme. In other terms, you can modify any configuration inside of it without going to edit the code...

7 minutes read.

Python Statistical Module

Python Statistical Module The Python statistical module provides various functions that we can use in our Python program, to perform mathematical statistics operations on the numerical data given to us. The...

8 minutes read.

Python GUI Programming

GUI (Graphical User Interface) GUI is a graphics-based operating system that uses icons and menus to interact with the user. Python mostly works on CLI (Command Line Interface). Widgets Any user interface has...

4 minutes read.

Multiple Linear Regression using Python

Linear Regression: Linear regression is a method that models the relationship between a dependent variable and one or more independent variables; in other terms, that models the relationship between a target...

6 minutes read.

Data Drop in Python

Introduction You'll understand how to delete a group of rows from a Pandas dataframe in this article.You can read this article on How to Drop Columns in Pandas to find out...

6 minutes read.