This is a text field

Python with a statement(instruction) and indentation

Python programming

Python has many great features. so, one of the features is the python with a statement.

The python-interpreter give the instruction to program. This instruction is a statement.

because of this, statements are crucial in python programming. so, this makes different types of statements. For example, print statement, if statement, while statement, for statement and multiline statement.

A Statement displays the output in the python programming language.

because of this, I’m going to use the print statement. For example,

print("Hello world")

output: Hello world

In the above example, you can see that the print statement displayed the output.

Assignment statement in python

message = "Hello world"
print(message)

output: Hello world

You can display the assignment statement.

because of this, the only value displayed.

Here, the variable only storing the value.

Multiline statement

number = (100+300+780+900+
          300+800)

if there is a statement that continued in the next line.

so, multiline statements occur.

because of this, you’ve to use +,/ operators to continue.

This operator continues the statement.

The statement with /. For example,

numbers = 200+500+/
600+400+/
300

In the above example, you can see that statement continued with / operator.

Identation in python

if statement:
.....print(statement)

Python indentation is a whitespace before the same block of code.

so, you can create a large block of code with python indentation.

because of this, you can put multiple statements in a block. See below,

if statement:
....print(statement1)
....print(statement2)

For indentation, you can give any number of whitespace.

But 4 spaces are recommended from python.org/pep/#indentation.

Indentation error in python

if statement:
....print(statement1)
......print(statement2)

output- Indentation Error: Unexpected indentation in block of code

In the above example, you can see that statement1 has 4 spaces and statement2 has 6 spaces.

so, you can see that the indentation level of statement1 and statement2 doesn’t match.

It reveals that both don’t belong to the same block of code.

But both are the same block of code.

so, the Indentation error occurs.

Indentation in python is useful in creating a long statement chain.

If you give different indentations for the same block of code. It’ll show you an indentation error in python.


Conclusion

Python statement is the instruction given to the interpreter.

There are many statements in python.

because of this, you can create a block of code.

This block of code contains whitespace.

The whitespace is known as an indentation in python.

In a block, the whitespace of two statements should same. Unless it creates an Indentation error in python.

So, what do you think about it? Is there anything I haven’t covered, you want to cover? Comment on it!

When you comment I and you both deeply understand it.

If you want to see more great blogs you can check out the below box.

Avatar for Vipul kunwar
Follow me

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Back to top