In this tutorial, we are going to learn about the statement, indentation, and comments in Python. Let's see all of them one by one.
The instructions written in Python are called statements. Let's see the following program.
print('Tutorialspoint') # statment
If you run the above code, then you will get the following result.
Tutorialspoint
The one line that was written in the program is called statement. Every line in the program is a statement. Can't we write a multi-line statement? Why not, we can.
We can write the multi-line statements using the backslash (\).
Let's see how to write multi-line statements in Python.
# multi-line statement result = 2 + 3 * \ 5 - 5 + 6 - \ 3 + 4 print(result)
If you run the above code, then you will get the following result.
19
Before talking about the indentation, let's see what is a block. A block is a set of statements instructions. Other programming languages like C, C++, Java, etc.., uses {} to define a block.But Python uses indentation to define a block.
So, how to write an indentation. There is nothing more than a tab. Each statement in the block must start at the same level.
def find_me(): sample = 4 return sample find_me()
If you run the above code, then you will get the following result.
4
def find_me(): sample = 4 return sample find_me()
If you run the above code, then you will get the following error as output.
File "<tokenize>", line 3 return sample ^ IndentationError: unindent does not match any outer indentation level
In the second program, the indentation level is not correct in the function block. So, we got an error. We must follow the indentation in Python. We can use the tab by default for indentation.
In Python, comments starts with hash (#) symbol. Let's see an example.
# This is a comment # This too... a = 4 # a = 5 print(a)
If you run the above code, then you will get the following result.
4
Unlike many other programming languages, there is no support for multi-line comments in Python. But, most of the people use docstrings as multi-line comments which is not a good practice.
If you have any doubts in the tutorial, mention them in the comment section.