How To Comment In Python

In this tutorial, we will discuss the Comment In Python. Python prides itself in being a very easily understood programming language.

When you are using Python to write code, it is very important that you make sure that your code can be understood by others quite easily.

There are many different ways to do this, and adding comments will definitely help with understanding codes!

This guide will help you to understand how to add comments in Python, improving the readability of your code! 

Also read: How To Make Tic Tac Toe In Python

How To Make Your Code Understandable

There are many different ways to make your code understandable to others. This can be done in the following ways: 

  • Giving variables very obvious names so that they are easily understood. 
  • Defining functions. 
  • Organizing your code. 
  • Adding comments.

This article will look at the adding comments option in making your code more understandable.

We will look at many different aspects of writing comments on your code.

Why it is important as well as how to word your comments in the most useful way.

Why Is It Important To Write Comments On Your Code

Comments are a very important part of understanding code in a programming language.

If you have a complicated function, then adding comments can help to explain these functions and shed light on the way that they work.

Having comments on your code can only help you to understand the code better, meaning that it will be a much more fluid development.

There are two instances where writing comments on your code is very useful.

These are when you are reading your own code and when others are reading your code. 

For Yourself

When you are reading your own code, you will sometimes find it difficult to keep up with what you are reading and how you got there in the first place.

While it is very tempting to leave out the stage of writing comments, thinking that if you have done it once you won’t forget how to unpick your code the second time around.

However, this is not always the case and it is very easy to forget how you got there. It is also a waste of time to have to work out the process all over again.

It will save you a lot of time and stress to just write your comments as you explain why and how these codes are the way they are.

It is a very common problem to forget what your code does and how you go there due to the pressure of deadlines.

Writing comments for yourself to prevent this problem from occurring is very important!

For Others

When other people are reading your code, adding comments to the code is very important.

If you are used to writing code that only you will see, you may be out of practice at adding comments to your code.

They take time and energy which you could be using elsewhere. However, sometimes there are situations where you need to be able to have others understand.

What the code means and how you got there.

For instance, if the project you are working on becomes a lot bigger than other programmers may need to be added to help ease your workload.

If this happens, they will need to understand your code.

Using comments throughout your work can really help them to understand the code a lot quicker, meaning they can help with the work a lot quicker.

This ensures that there is a smooth transition between developers.

How To Add Comments In Python

Comments help developers to understand what the programmers intentions were when they wrote the code.

Writing a comment in Python is very easy. All you have to do is add a # before the comment that you wish to leave.

See sample code below:

#!/usr/bin/python

number = 100                  # An integer assignment
score   = 98.5                   # A floating point
name    = "Prince"          # A string

print counter
print miles
print name

Python won’t try to use the information that you put in after a hash mark, and up to the end of the line.

Instead, this will be ignored in the code but left as a comment to help people to understand why it is there. You can insert these comments anywhere within Python.

Good Commenting Practices 

It is very important to write your comments in clear, plain English. It is likely that a developer will only read over the comments if they need some guidance on the code that they are reading.

Because of this, it is important to use very clear language so that the comments will help them to understand, rather than complicating them further.

It is a good idea to make sure that your comments are very short, understandable and useful.

Try not to add rambling comments that are not necessary.

Python has a 72 character limit on comments for one line of code. 

You should practice writing comments to make sure that you are writing understandable and appropriate comments.

It is a good idea to write some comments and ask a colleague to run through your code to ensure that they understand it.

It is also quite important to sign off your comments with your initials to ensure that if anyone has any questions, they can ask you directly for clarification

Go back on old code that has no comments and try adding comments to that. This will help you to practise the skill.

It is a good idea to use similar language throughout the code so that once you are tuned into it, it is very easy to understand.

If you are explaining something that links back to another comment, it is beneficial to use references and language that makes this clear.

Final Thoughts 

Adding comments to your code is a valuable tool that will help you to gain a deeper understanding of Python, as well as helping you to write more clearly and concisely. 

Comments are useful for both you and others and so are a great tool to know how to add!

Hopefully, now that you have read this guide, you understand how to add comments in Python and the pros of adding these.

Leave a Comment