This meme about comments in Programming is very popular among all programmers, maybe the same for you! When reading others’ programs, have you ever felt like “What the hell is this block of code doing”? Wouldn’t it be helpful if the code you’re reading includes a slight description of each block? The concept of comment comes here to save you!
Although Python’s clean syntax helps you to understand the structure of the code, you sometimes need a slight description to understand the purpose of the code, and the flow of it. To describe a part of the code, every programming language allows a special syntax, that tells the compiler not to execute that part and to skip to the next part.
Comments can be categorized into 2 major categories: single line comments, and multi-line or paragraph comments. In this tutorial, you’ll learn about all the possible ways of declaring comments in Python.
Syntax of comments in Python
In a nutshell, Python includes a few types of comments.
- Hash or Pound symbol
# This is a comment # Comments are ignored by the compiler print('This line will be executed by the Compiler') # This part is also a comment
- Unassigned String Literals
'This is a string but Python will ignore it' "Because Python treats an unassigned string as a comment" text = 'This is an assigned string, so will not be ignored by Python' print(text)
- Triple Quotes – Single (*)
'''This is a paragraph comment can be expanded to as many lines as you wish''' print('Only this line will get printed')
- Triple Quotes – Double (*)
"""The same as before just uses 3 double quotes instead of 3 single quotes""" print("Only this line will get printed")
Although these seem very easy, they can get very confusing over time. So continue reading below and when reading about the 3rd and 4th options, read the next blog on Multiple Lines or Paragraph Printing in Python so that you don’t get confused!
Single Line comments in Python
When your comment is short and spreads only one line, you can use options 1 and 2 stated above.
Pound symbol for commenting
When you place a pound sign before any line, Python considers that line as a comment and ignores it. A comment is not to tell Python what to do, it’s for you to understand later what you were doing earlier.
# Display my name with greeting print('Hello everyone! My name is S. M. Fahim') # Time of writing this blog print("It's 3.28 AM in the morning.")
Here’s the output:
Remember, if you wrap the pound symbol inside quotes, Python won’t consider it as a comment, rather will consider as a string.
# This is a comment print('This is not a #comment') # quotes made it a string print("Notice, # This is not a comment")
Unassigned strings behave as comments
You can assign a string to a variable to use it later, or assign it to a function to perform some tasks. But when you leave it alone, i.e. don’t assign the string to anything, Python considers it as a comment and ignores it.
'Unassigned string 1' "Unassigned string 2" text = 'Assigned string 1' print(text) print('Assigned string 2')
This looks cool, but rarely used in practice. Maybe to avoid the conflict with string literals.
Sometimes you may need to comment within a line along with the code. The pound symbol helps you here. But remember one thing, the inline comment must be at the end of the line.
print('Hello, World!') # a simple example # another simple example print('Hello again') # and another; print('Unfortunately the world has currently stopped responding')
Join the Facebook Group if You Haven’t Yet!
Multiline/Paragraph comments in Python
For multiline/paragraph comment, you can use any of the 4 options stated above. Either use a pound symbol before each line, or use triple quotes to wrap the entire comment. Let’s dive deep into it.
Block comment with pound symbol
This one is very common in practice. Just keep adding pound symbols before each line. You’ll mostly see it at the beginning of some scripts describing the entire script briefly.
# This is a simple Python function # The function takes 1 input as x # Returns the value of y when y = 3x^2+2x+5 def calculate(x): y = 3*x**2 + 2*x + 5 return y print(calculate(10))
Don’t worry if you don’t understand the code yet. I’ll cover function soon. The output of the code:
Triple Single Quotes
If you wrap the comment text with triple single quotes, then Python considers the entire text as one comment. It doesn’t matter how many lines that comment spreads. Whenever Python sees starting triple quotes, it looks for ending triple quotes. Triple quotes are mostly used as docstring, i.e. the comment at the beginning of a function, module, class, or method.
'''This is a simple Python function The function takes 1 input as x Returns the value of y when y = 3x^2+2x+5 ''' def calculate(x): y = 3*x**2 + 2*x + 5 return y print(calculate(10))
The output is the same as before.
Triple Double Quotes
The same as before,. You’re allowed to use either single or double quotes, just use 3 of them. But don’t mix them.
"""This is a simple Python function The function takes 1 input as x Returns the value of y when y = 3x^2+2x+5 """ def calculate(x): y = 3*x**2 + 2*x + 5 return y print(calculate(10))
Triple quotes (either single or double) are used for 2 different tasks.
- To create comment
- To create paragraph string
Read the next blog right now so that you don’t get confused of them.
A few notes about comments in Python
- You can use comments to test your code. Commenting a part of your script will neutralize it and hence Python will escape that part.
- Comments should be short and concise
- Don’t make unnecessary comments. It’ll make your code tough to understand by ruining the structure.
- Don’t make comments where the code is already very clean to understand
- Write descriptive comments for parts that are very tough to understand