×

Python comment symbol

Python programmers frequently use the comment system because, without it, things may quickly become very perplexing. The developers' helpful information is provided in the comments, which helps the reader understand the source code. The logic, or a portion of it, employed in the code is explained. In most cases, comments are beneficial to someone who will maintaining or improving your code after you are gone and unable to provide support. These are frequently mentioned as a helpful programming practices that do not affect the program's output but enhance readability across the board. In Python, comments refer to lines of code that the interpreter skips over while the programme is running. The use of comments makes the code easier to read and aids in thorough code comprehension by programmers.

Comment symbol: The following categories of comments with various notations or symbols are available in Python as different forms of code comments.

  • Single-line comments
  • Multi-line comments
  • Docstring comments

Let us talk about each of the several types of symbols that are used in the above type comments.

1) Single-line comments

Python single-line comments begin with the hashtag symbol (#) and continue until the end of the line. If the comment spans more than one line, add a hashtag to the following line before continuing. Short explanations of variables, function declarations, and expressions can be provided using Python's single-line comments.

Let us look at a programme that illustrates how to utilise the comment symbol for single-line comments:

# print “Hallowman” for understanding
print(“Hallowman”)

Output

Python comment symbol

The hashtag (#) symbol is a comment that is used in single-line comments in Python programming.

2) Multi-Line Comments

Python does not offer multiline comments as an option. But there are other methods we can use to create multiline comments. It can be achieved by two ways.

  • Using Multiple Hashtags (#) symbols
  • Using String Literals symbols

Using Multiple Hashtags (#) symbols: Python allows us to write comments with several lines using multiple hashtags (#). The comment will be treated as a single line for every line.

An example of a multiline comment containing numerous hashtags (#) symbols.

# demo program to show
# Multiline comments
print(“it is a multiline comment”)

Output:

Python comment symbol

Using String Literals: We can use string literals as comments because Python ignores those that are not allocated to a variable.

Let us understand with ease using string laterals:

Example 1: The words in between these symbols are ignored

We can therefore use the following as a single-line comment:

Here, #it can be a comment

In both ways, these symbols are used as multiline comments. The second line of the programme is a string, as seen above, but it is not allocated to a variable or function. Thus, the string is ignored by the interpreter. Similarly, multi-line comments can be written using multi-line strings (triple quotes). It is possible to use either 'or' as the quotation character.

Since we know see there won't be any output after running the code above, we use the triple-quoted strings (""") as multiline comments.

Example 2:

""" a simple Python program to show
 multiline comments"""
print(“it is a multiline comments”)

Output:

Python comment symbol

The interpreter disregards the multi-line string because it isn't set to a variable in this case. It can be used as a multi-line comment even though it isn't officially one.

3) Docstring comments

The string literals enclosed in triple quotes that follow the function in Python are known as docstrings. It is used to link the written documentation to the classes, methods, functions, and modules of Python. It is included immediately after the functions, modules, or classes to explain what they perform. Following that, Python's __doc__ property makes the docstring accessible.  as in the above method, or function definition, "triple single quotes" or "triple double quotes" are used to declare the docstrings. There should be a docstring for each function. The help function or the object's __doc__ method can be used to obtain the docstrings.

Here triple quote symbol is used for the python code comment.

Let us have a demonstration program to know briefly.

Example 1:

def add(s, h):
    """Adds the value of s and h"""
    return s+h
# Print the add function's documentation string.
print(add.__doc__)

output:

Python comment symbol

Example 2:

def my_function():
	'''Demo of the triple double quotes
	docstrings and does nothing really.'''
	return None
print("consuming __doc__:")
print(my_function.__doc__)


print("consuming code:")
help(my_function)

output:

Python comment symbol

Related Topics

Loan Calculator using PyQt5 in Python

In the following tutorial, we will learn how to build a Loan Calculator application using the PyQt5 library in the Python programming language. So, let's get started. Introduction to the code: The heading...

4 minutes read.

How to check data type in python

The data type represents the nature of a variable; it determines what kind of data it can store and what operations can be carried out on it. There are five...

4 minutes read.

Python Dictionary

Dictionary in Python is an unordered collection of data values, which is used to store data values like a map. Unlike different data types that hold just individual assets as...

4 minutes read.

How to Create an Array in Python?

How to Create an Array in Python In the previous article, we have discussed how to declare an array in Python. So, let's have a quick revision of that, and then we...

5 minutes read.

Python Knapsack problem

Python Knapsack problem Before we dig down about Knapsack problems in Python, first let's have a look at what is actually a knapsack problem. What is a knapsack problem? A problem from the...

5 minutes read.

Python try catch exception

The try-except proclamation can deal with exceptions. Exceptions might happen when you run a program. Exceptions are blunders that occur during the execution of the program. Python won't educate you regarding...

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.

AES CTR Python

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

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

chr() and ord() Functions in Python

Python language contains many built-in functions which we use for many programs to work efficiently. The chr() and ord() are a few built-in functions in Python that are used for...

4 minutes read.

How to import pandas in python

How to Install Pandas in Python Python is a vast ocean of libraries, modules, and different functions. It has a solution for almost everything. Using Python, we can simplify even a...

3 minutes read.

Python Tutorial

Python tutorial is a widely used programming language which helps beginners and professionals to understand the basics of Python programming easily. Python is a high-level, easy, interpreted, general-purpose, and dynamic programming...

19 minutes read.

How to Install Python

Python Installation Guide Step by Step Installing Python is quite simple and easy. In this tutorial, we will show stepwise procedure to install Python and set up the Python environment in different...

2 minutes read.

Python Kwargs Example

In this article, we'll talk about Python's kwargs notion. In Python, kwargs has two stars and passes a variable number of keyworded argument lists to the function, whereas args has...

5 minutes read.

Python String rjust() method

Python String rjust() method The string.rjust() method in Python returns a right-justified string of a given minimum width where the padding is done using the specified fillchar (default is a space). It returns...

2 minutes read.

BOTTLE Python Web Framework

The Bottle is a lightweight WSGI micro web framework for python. It acts like a thin wrapper around a web server where it is distributed as a single file module...

4 minutes read.

Hypothesis Testing in Python

Hypothesis Testing in python is widely used along with statistics. Many libraries in Python are very useful for statistics and machine learning. Libraries like numpy, scripy etc. help in hypothesis testing in...

12 minutes read.

Python Parallel Processing

By performing more jobs concurrently, your software may complete more tasks in a shorter amount of time. These aid in solving major issues. The following subjects will be covered in...

2 minutes read.

Python abs() function

Python abs() function The abs() function returns the absolute value of a number. Syntax abs(x) Parameter x: The parameter ‘x’ can be an integer value, a floating point number or a complex number. Return This function returns...

1 minute read.

Reverse a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.