We use cookies. More

Python Comments: How to Write Comments in Python

Like in every other programming language, you can have comments in Python documents to help you during and after the development process. In this quick tutorial, I will show you how comments work in Python.

Advertisement

Comments are a crucial part of any type of programming activities that are not only helpful to the person or group who is carrying out the development work but also to any coders who may take over the project or want to modify it in the future. Though the comments are usually written by the coder as a future self-reference to quickly remember the purpose of that code piece and sometimes only understandable by its author, they should be written in a way any coder would easily understand the comment, as well as give a clue about what the code it refers to does.

If you are a new coder, especially if you have just started to learn Python, don't worry much about perfecting how you write comments. As you will also notice in time, adding relevant, useful and informative comments is a skill that you improve as you practice your coding more and realize which parts require commenting and which parts don't. When you master how to comment while coding, you will be producing properly commented documents and files that will give the sufficient amount of information to anyone who inspects them at a later time.

Python Comments: How to Write Comments in Python

For example, you don't need to add comments to very basic scripts such as a Hello World script or a simple function that adds two integers as they are pretty straightforward to figure out what they are about. However, if you are building an admin panel with a lot of features and functions of thousands of lines, then you should be adding proper comments where necessary.

Also Check:
How to Add Comments in HTML
How to Add Comments in CSS

Now, let's see how Python's comment syntax is.

Python Comments Syntax: How to Add Comments in Python

As in other programming languages, you can have single-line comments, inline comments or multi-line comments in Python depending on your commenting needs. The following examples will help you better understand how commenting in Python works.

Single-Line Comments in Python

In Python, the single-line comment lines start with a # character. Let's say you have a sample script.py file, you can insert the following code into it to get accustomed to Python comment syntax.

# This is a comment.
var1 = 5

You will need to use the # character at the beginning of each comment line if you prefer to add multiple single-line comments.

Inline Comments in Python

One of the quite useful things about comments is that you can place inline comments right at where they will make most sense. In Python, inline comments also start with a # character. The following is an example for an inline ocmment in Python.

var1 = 5 # Add comment here.

Multi-line Comments in Python

For multi-line comments, three double quotes are used like the following:

""" This is comment line 1
This is comment line 2
This is comment line 3 """

This concludes our tutorial about Python comments and hopefully it will be helpful in your Python programming projects.

Also Check:
Free FTP Software
Download a File with PHP
HTTP Status Codes
Free Stock Images

Advertisement

Was this post helpful?

If you found this post to be helpful, please share it! Thank you :)

f t g+ in