×

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

STL in Python

Python has many libraries that are very useful and simplify many complex codes. In the same way, STL is also one of the python libraries used for reading and writing...

3 minutes read.

Kite Python

Kite in Python: The Kite is a package provided by the python programming language; it works with the help of artificial intelligence and helps us write code inside the visual studio....

3 minutes read.

The Calendar Module of Python

The calendar module provides calendar features, including printing functions for a specific month or year. Default is the first day of the week on Monday and the last day on Sunday...

3 minutes read.

How to Update Python?

How to Update Python In this article, we will discuss how we can update Python in our system. For a better understanding, this article will cover all the steps right from installation...

4 minutes read.

Python Algorithms

A quote goes, "A goal without a plan is just a wish." To achieve anything in life, we can't simply go for it without making a plan and sticking to...

4 minutes read.

Python globals() function

Python globals() function The globals() function in Python returns the value of the named attribute of object where the ‘name’ must be a string. Syntax globals() Parameter NA Return This function returns the global symbol table as a dictionary. Example...

1 minute read.

How to make API calls in Python

Information is extremely critical these days since it drives applications and organizations. It is subsequently critical to figure out how to get this information to serve your application. In fundamental...

4 minutes read.

Python Recursion

Recursion is one of the most interesting yet important concepts of any programming language. If you want to be a good programmer or data scientist, then you should better have...

4 minutes read.

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

How to check version of Python

How to check version of python The versions of Python come with different kinds of features and functionalities. It is not a herculean task to keep track of the updates these...

3 minutes read.

Python logging Module

Python logging Module Introduction By logging word we understand the tracking of the events which happens when we run some software. Logging process is very important part for developing software, debugging...

12 minutes read.

Gaussian elimination in python

Linear and polynomial equations are used in almost all fields of numerical simulation. However, its most common use in engineering is in the area of linear system analysis. The broader...

3 minutes read.

Python callable() Function

Python callable() Function The callable() function returns a boolean value ‘True’ if the specified object is callable, else it returns False. Syntax callable(object) Parameter Object:  The object parameter represents the value to test if it is callable...

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

Python Skyline

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.

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 Maurer Rose and Rhodonea Curves

In this tutorial, in Python we will make a Maurer Rose example and Rhodonea Curve example. Before we continue to see what precisely is maurer rose or a rhodonea bend...

10 minutes read.

Read Text files in Python

In Python, there are many ways to read text files. Before going into the detailed structure of reading a text file, let us understand how reading text files takes place...

4 minutes read.

Python Uses

Python has advanced in recent years to rank among the programming languages that are most often used globally. It is utilized in everything, including machine learning, software testing, and website...

4 minutes read.

Check if a String is Empty in Python

The string is one of the data types of Python. This data type stores all kinds of data but all the characters must be enclosed using double quotes (""). In...

5 minutes read.