How to use Python Comments – with example

  • Reading time:15 mins read

What is a Python comment?

In Python, comments are denoted with a hashtag symbol. And whatever is written after this will be ignored by the code editor.

Python comments can be used to serve different purposes when used in code, for instance, they can be used to make code more readable, or to prevent the execution of a piece of code.

 

 

For example, if we create a simple function which is called ‘add’ and we give it to numbers and it returns some of those numbers, x plus y. This is quite a simple function.

 

python comment

 

However, we might want to put in here a comment to remind ourselves of what we did and we could use this with the hashtag symbol which says this adds x and y values together. Now when someone goes back to your code, they can easily see what this function does.

 

 

Although this is really a simple function, imagine this was a complicated function with lots of things going on. Python comments would be really useful.

It is a good habit for developers to write Python comments that explain what their code is doing, as mentioned earlier comments can also be used to stop some part of the code from running.

Maybe if we had a print statement just to test what was going on with the code. So we could say, let’s run this with some values 5 and 8.

 

 

Now when we run this, we’re actually going to get the printed value out because we are printing it inside the function.

But let’s say that we now don’t want the print function to run within the function and we want it to stop running for whatever reason, but we want to leave it handy so you can go back to it if need be.

 

python comment

 

Well, comments allow us to print out the whole line out and now we will run this. We’re not going to get anything printed back because we have not got a print statement and we are simply returning the value.

 

 

That’s the next best use for comment in Python to comment out bits of code that we don’t want to run. Now, Python comments come in a few different shapes or forms. These are single-line Python comments, we might want to do multiline comments.

 

python comment

 

Maybe you want to write some more detailed information about what we are trying to do in our code. Now in order to achieve this, we could do a single line comment in every line. 

 

 

But a more simple way to do this is by using multi-line comments which are usually represented using triple quote marks. And now anything we put in between these two is going to be ignored by the code editor when it runs.

 

 

This is more useful when you have more to say or you need to actually write something at the top.

Summary

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.