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.
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
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
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.
