How to write Comments in Python3?
Last Updated :
31 Jul, 2023
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:
Output :
GFG
Comments should be made at the same indent as the code it is commenting on.
python3
def GFG():
print("GeeksforGeeks")
for i in range(1, 2):
print("Welcome to Comments in Python")
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():
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
def addition(a, b):
c = a + b
return c
a = 10
b = 3
sum = addition(a, b)
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")
else:
print("Debugging")
|
Output :
True
Also, check:
Learn Python 3 at your own pace with our well-structured tutorial: Python 3 Tutorial
Similar Reads
How to write Comments in Python3?
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 mai
4 min read
Python | os.write() method
OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.write() method in Python is used to write a bytestring to the given file descr
2 min read
Python | os.writev() method
OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.writev() method in Python is used to write the contents of specified buffers t
2 min read
Python - Write Bytes to File
Files are used in order to store data permanently. File handling is performing various operations (read, write, delete, update, etc.) on these files. In Python, file handling process takes place in the following steps: Open filePerform operationClose file There are four basic modes in which a file c
2 min read
Writing to file in Python
Writing to a file in Python means saving data generated by your program into a file on your system. This article will cover the how to write to files in Python in detail. Creating a FileCreating a file is the first step before writing data to it. In Python, we can create a file using the following t
4 min read
turtle.write() function in Python
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.write() This function is used to write text at the current turtle position.
1 min read
How to create constant in Python
Python lacks built-in constant support, but constants are typically represented using all-uppercase names by convention. Although they can technically be reassigned, itâs best practice to avoid modifying their values. This convention helps indicate that such variables should remain unchanged through
2 min read
Write a dictionary to a file in Python
Dictionary is used to store data values in the form of key:value pairs. In this article we will see how to write dictionary into a file. Actually we can only write a string to a file. If we want to write a dictionary object, we either need to convert it into string using json or serialize it. Method
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 cre
3 min read
Python PRAW - Downvoting a comment in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. We can either upvote or downvote a comment. Here we will see how to downvote a comment using PRAW. We will be using the downvote() method of the Comment class to downvote a comment. d
2 min read