Statements, Comments, DocString and Indentation in Python
|In this Python article, we will learn about indentation, statements, comments and will also implement some examples.
1. Comments
When we want to put a note or description in our code so that other people can understand the code, we use Comments. Comments are pieces of code that the compiler ignores or simply does not execute.
Note: As a good coding practice we should try to write the code in a way that the code is self-explanatory and we do not need comments to explain our code. But sometimes the situations are tricky and we should add comments such that it compliments the code.
Let’s discuss and implement the two ways to insert comments in a python program.
1.1. Single line comments
The comments that begin with #
are single-line comments and the compiler will not execute anything written after #
in that specific line.
Let’s use single line comments in a python program.
#In this program, we are printing a quote print("It's a beautiful day to save lives")
Output It's a beautiful day to save lives
So the example above, the compiler ignored the first line which we used to keep a note of what does this program does.
But if you want to write a comment of more than a line example about the date of creation, author, description etc. For this, we have a multi-line comment in python.
1.2. Multi-line comment
If we want to write multiple lines of comments then we can simply use #
multiple times, right?
# Date-of-creation : 27-01-2021 # Author: Codingeek # Description: In this program, we are printing a quote print("It's a beautiful day to save lives")
Output It's a beautiful day to save lives
Comments are used to make code more readable and this doesn’t look good, and we have to use #
every time we are writing a new comment.
But no issue, python has a solution for it that is easier than the above method. So, to create a multi-line comment in python we need to mark the beginning and end of the comments with either three double quotes(""")
or three single quotes(''')
.
"""Date-of-creation : 27-01-2021 Author: Codingeek Description: In this program, we are printing a quote""" print("It's a beautiful day to save lives")
'''Date-of-creation : 27-01-2021 Author: Codingeek Description: In this program, we are printing a quote''' print("It's a beautiful day to save lives")
Both the programs above will generate the same output, we just have used two different methods to write multi-line comments.
Output It's a beautiful day to save lives
1.3. Docstring
Docstring is written the same way as we write multi-line comments using either three double quotes(""")
or three single quotes(''')
.
Docstring is used for the official documentation of our code. This documentation can later be used to generate guides and documentation in different formats like HTML, pdf, etc.
We can use docstring in the class, function, method, etc. It should be the first statement in the object’s definition so that the other developer or consumers of that code, library, API can understand the code and meaning of parameters if any.
Let’s understand docstring using an example:
def quote(): ''' This function prints a quote. ''' print("It's a beautiful day to save lives.") print("the docstring value..") print(quote.__doc__)
Output:- the docstring value... This function prints a quote.
REMEMBER : Python docstrings are not comments
You can check the docstring of any object in python using __doc__
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> sum.__doc__ "Return the sum of a 'start' value (default: 0) plus an iterable of numbers\n\nWhen the iterable is empty, return the start value.\nThis function is intended specifically for use with numeric values and may\nreject non-numeric types."
2. Difference between Docstring and Comments
Let’s discuss how docstring is different from comments in Python.
DocString | Comments |
---|---|
Helps in understanding the larger part of code like the purpose of a function, class etc. | Helps in understanding the smaller part of code like understanding a statement, expression etc. |
Can be accessed by code using __doc__ | Can’t access it within the code |
Should be written after a function or class or on top of a module | Can be written anywhere in the program |
Enhanced and logical. Used for generating guides and documentation. | Explain the non-obvious portion, helps in fixing bugs |
3. Statements
Statements are the instructions given to a computer to perform different tasks be it data modifications, making decisions, or repeating actions. There are different types of statement in Python
a = 1 #assignment statement for i in range(10): #compound statement pass
Compound statements are a group of statements in a block and we can achieve it by indentation. if
statement, while
statement, for
statement, try statement, with
statement, function
definition, class
definition are some of the examples of compound statements.
- Multiple statement in a single line using
;
a =1 ; print("Hello"); print("HI")
Output Hello HI
- Breaking a single statement into multiple lines
print("It's a \ beautiful day \ to save \ lives ")
Output It's a beautiful day to save lives
How does the compiler know that this is a whole block or it’s a compound statement? So for this, we talk of a concept called Indentation.
4. Indentation
Indentation is basically white spaces before the statements which help the compiler to understand the python program. For writing compound statements, every statement should have the same no. of white spaces or tabs before the statements.
def quote(): print("Quote by Derek Shepard-") print("It's a beautiful day to save lives.") quote()
Output Quote by Derek Shepard- It's a beautiful day to save lives
In the above example, the python compiler knew the function definition has ended because of the indentation. If the indentation is miswritten, the compiler will give an error known as Indentation error.
def quote(): print("Quote by Derek Shepard") print("--------------") print("It's a beautiful day to save lives.") quote()
Output File "c:\Users\parih\Desktop\Kaggle\anime-Task-1\testing.py", line 4 print("It's a beautiful day to save lives.") IndentationError: unexpected indent
5. Conclusion
In this article, we discussed single line and multi-line comments, what is a docstring, and how it is different from comments, different types of statements, Python indentation, and Indentation error.
Helpful Links
Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.
Also for examples in Python and practice please refer to Python Examples.
Complete code samples are present on Github project.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?