In this tutorial, we will discuss some important concepts of Python, which are very important and most used to describe the procedure of Python code. so let’s discuss Python Comments Statements Indentation Docstring.
In Python, usually, you will come to hear the word “Python Statement”. What does it mean?
In simple words, any instruction that can be executed by the Python interpreter whether it gives an error or not is known as a statement.
For example, var=40 is a statement and there are many other statements such as if statement, while statement and many more which we will discuss later.
In other high-level programming languages when we write a block of code, which belongs to a single statement, we use braces to hold that code. In Python, we do not use braces we use indentation. For indentation, you can use white space or just press tab button.
Let’s understand it with an example:s
#syntax of a for loop using indentation
for i in range(1):
print("this is a part of For statement") print("we have use indentation before printing this statement") print("THIS IS NOT A PART OF FOR STATEMENT we haven’t used indentation here")
this is a part of For statement
we have use indentation before printing this statement
THIS IS NOT A PART OF FOR STATEMENT we haven’t used indentation here
Python Comments (#)
In Python there is a concept known as Python comments, which play an important role whenever you write any code and do not want it to execute or run, there we use comments. The Python interpreter just ignores the comment and move to the next line of code. In Python, whether it is a code or a normal text, you can make a comment by using a special symbol hash (#).
The question is why do we need something which does not do execute, in every programming language you will find this concept of comment and they are designed for a purpose, i.e. suppose if a third person check your source code or code how will he/she supposed to know that what a specific line or function or class is going to do to, there we use comment and add some text information. So that the third person can understand why we have declared or write this code.
Let’s take an e.g.
# this is a comment
# and the Python interpreter is not going to execute it
print(“ Hello World”)
#This statement will print Hello World and this is a comment
#print(“Hello World 2”)#
Python interpreter will not execute this statement because we #have used (#) before the print() statement so it’s a comment now
In the above example, we have seen that if we use hash(#) to comment something so its valid for a single line what If we want to comment multiple lines for that we use triple quotes you can use ”’ or “”””to make a multiple line comment statement.
Let’s understand it with an example.
# this is a single line comment using hash (#)
# to make this line a comment we again used hash (#) at the beginning of this line
“”” this is a multi-line
This code won’t be executed
because it is inside the multi-line comment
this line is the last line of the multi-line comment because we have used “””
Docstring stands for documentation string, it is look like a multi-line comment but has a different purpose to use. A docstring is used inside a statement, function, and class and to give some additional information about those.
To use a docstring, we use triple quotes either “”” or ‘’’
The only difference between a docstring and comment is you can call the docstring text by using the special keyword __doc__ .
Let’s understand it with an example.
def print_hello(): ''' this is a doc string of function print_hello which is used to print hello''' print("hello") print(print_hello.__doc__)
This is a doc string of function print_hello which is used to print hello
- Getting Started
- Keywords and Identifiers
- Python Variables
- Python Datatypes
- Python Type Conversion
- Python I/O and import
- Python Operators
- Python Namespace