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.
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.
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.
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.
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 Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
- How to create perfect HTML tables?
- HTML color codes
- CSS background images
- Best Programming Books You Must Read in 2021
- Python for loop
- Creating a table using HTML and CSS
- Best way of using Java Arrays and ArrayLists
- Best way of using Python Sets
- Best ways of using a Python Dictionary
- Best way of using Python Classes
- Best way of using Python Range
- Best way of using Python if-else
- Best way of using Python RegEx
- Best way of using Python Lists
- Best way of using Python Enumerate
- Best way of using Python Functions
- Best way of using Python Split
- Best way of using Python Try-Except
- Best way of using Python Tuple
- Best way of using Python Arrays
- Best way of using Python Sort
- Best way of using Python DateTime
- How to download Python?
- Best way of using Python FileWrite
- Best way of using Python Lambda
- Best way of using Python ListAppend
- Best way of using Python ListComprehension
- Best way of using Python Map
- Best way of using Python Operators
- Best way of using Python Pandas
- Best way of using Python Requests
- Best way of using Python Strings
- Best way of using Python Count
- Best way of using Python Comments
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.