Open In App

How to write Comments in Python3?

Last Updated : 31 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Comments are text notes added to the program to provide explanatory information about the source code. They are used in a programming language to document the program and remind programmers of what tricky things they just did with the code and also help the later generation for understanding and maintenance of code. The compiler considers these as non-executable statements. Since comments do not execute, when you run a program you will not see any indication of the comment in the output.

Syntax: The hash(#) symbol denotes the starting of a comment in Python.  

# This is a comment in Python

Example:

python3




# This is the syntax of a comment in Python
print("GFG")
  
# Comments dont have any effect on the interpreter / output


Output :  

GFG

Comments should be made at the same indent as the code it is commenting on.

python3




def GFG():
    
  # Since, this comment is inside a function
  # It would have indent same as the function body
  print("GeeksforGeeks")
    
  for i in range(1, 2):
  
    # Be careful of indentation
    # This comment is inside the body of for loop
    print("Welcome to Comments in Python")
    
# This comment is again outside the body
# of function so it wont have any indent.
  
print("Hello !!")
GFG()


Output

Hello!!
GeeksforGeeks
Welcome to Comments in Python

Types of Comments

1. Single-Line Comments: Comments starting with a ‘#’ and whitespace are called single-line comments in Python. These comments can only stretch to a single line and are the only way for comments in Python. e.g.

# This a single line comment.

2. Multi-line (Block) comments: Unlike other programming languages Python doesn’t support multi-line comment blocks out of the box. However we can use consecutive # single-line comments to comment out multiple lines of code. Some examples of block comments-

# This type of comments can serve
# both as a single-line as well
# as multi-line (block) in Python.

3. Inline Style comments: Inline comments occur on the same line of a statement, following the code itself. Generally, inline comments look like this:

x = 3        # This is called an inline comment
a = b + c # Adding value of 'b' and 'c' to 'a'

4. Docstring comments: Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods. It’s specified in source code that is used, like a comment, to document a specific segment of code. Unlike conventional source code comments, the docstring should describe what the function does, not how.

Example: 

python3




def my_function():
    """Demonstrates docstrings and does nothing really."""
  
    return None
  
  
print("Using __doc__:")
print(my_function.__doc__)
  
print("Using help:")
help(my_function)


Output: 

Using __doc__:
Demonstrates docstrings and does nothing really.
Using help:
Help on function my_function in module __main__:
my_function()
Demonstrates docstrings and does nothing really.

Advantages and Uses of Comments:

Planning and reviewing: In the comments, we can write the pseudocode which we planned before writing the source code. Pseudocode is a mixture of natural language and high-level programming language. This helps in reviewing the source code more easily because pseudocode is more understandable than the program. 

Example:

python3




# This function is adding two given numbers
def addition(a, b):
    
  # storing the sum of given numbers in 'c'.
  c = a + b
    
  # returning the sum here
  return c
  
# passing the value of a and b to addition()
a = 10
b = 3
sum = addition(a, b)
  
# printing the sum calculated by above function
print(sum)


Output :

13

Debugging: The brute force method is a common method of debugging. In this approach, print statements are inserted throughout the program to print the intermediate values with the hope that some of the printed values will help to identify the errors. After doing debugging we comment on those print statements. Hence comment is also used for debugging.

python3




a = 12
  
if(a == 12):
  print("True")
    
# elif(a == 0):
  # print("False")
    
else:
  print("Debugging")


 
Output :

True

Also, check:

Learn Python 3 at your own pace with our well-structured tutorial: Python 3 Tutorial



Similar Reads

Interesting Fact about Python Multi-line Comments
Multi-line comments(comments block) are used for description of large text of code or comment out chunks of code at the time of debugging application.Does Python Support Multi-line Comments(like c/c++...)? Actually in many online tutorial and website you will find that multiline_comments are available in python(""" or '''). but let's first look at
3 min read
Comments in Ruby
Statements that are not executed by the compiler and interpreter are called Comments. During coding proper use of comments makes maintenance easier and finding bugs easily.In Ruby, there are two types of comments: Single – line comments.Multi – line comments. Here, we are going to explain both types of comment with their syntax and example: Ruby Si
2 min read
How do we create multiline comments in Python?
Comments are pieces of information present in the middle of code that allows a developer to explain his work to other developers. They make the code more readable and hence easier to debug. Inline Comment An inline comment is a single line comment and is on the same line as a statement. They are created by putting a '#' symbol before the text. Synt
3 min read
How to Extract YouTube Comments Using Youtube API - Python
Prerequisite: YouTube API Google provides a large set of API’s for the developer to choose from. Each and every service provided by Google has an associated API. Being one of them, YouTube Data API is very simple to use provides features like – Search for videosHandle videos like retrieve information about a video, insert a video, delete a video et
2 min read
Integrating Facebook Comments Plugin in Django Project
Django is a Python-based web framework that allows you to quickly create efficient web applications. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database – SQLlite3, etc. In this article, we will learn to integrate the Facebook comment plugin in Dj
2 min read
Integrating Facebook Like, Comments and Share Plugin in Flask Project
Flask is a Framework of Python that allows us to build up web-applications. It was developed by Armin Ronacher. Flask’s framework is more explicit than Django’s framework and is also easier to learn because it has less base code to implement a simple web-Application. This article revolves around how to integrate Facebook comments plugin in flask ap
2 min read
Django project to create a Comments System
Commenting on a post is the most common feature a post have and implementing in Django is way more easy than in other frameworks. To implement this feature there are some number of steps which are to be followed but first lets start by creating a new project. How to create Comment Feature in Django?Open command prompt and run the following commands
6 min read
Converting Jinja Comments into Documentation
Developers often face the challenge of creating comprehensive documentation for their code. However, by leveraging Jinja, a powerful template engine for Python, it becomes possible to automatically generate documentation from code comments. This article will explore the process of using Jinja to extract information from code comments and create str
5 min read
Python Comments: Importance, Types and Correct Way to Use
Comments in Python are the lines in the code that are ignored by the interpreter during the execution of the program. # I am single line comment """ Multi-line comment used print("Python Comments") """ Comments enhance the readability of the code and help the programmers to understand the code very carefully.
4 min read
Multiline comments in Python
In this article, we will delve into the concept of multiline comments in Python, providing a comprehensive definition along with illustrative examples in the Python programming language on How to Comment Multiple lines in Python. What is a Multiline Comment in Python? Multiline comments in Python refer to a block of text or statements that are used
4 min read
Article Tags :
Practice Tags :