Robotics & Artificial Intelligence
Why indentation in Python is important? Explain with the help of an example.
Getting Started
3 Likes
Answer
Indentation refers to the whitespaces or tabs that are used at the beginning of a block of statements. The statements or instructions with the same indentation belong to the same block, i.e., multiple statements with the same indent are called a block of code. Indentation is used in Python to include all the statements belonging to a particular block. Inappropriate indentation may result in a syntactical or logical error.
Example:
i = 1
if i == 1:
print("i is equal to 1")
else:
print("i is not equal to 1")
Here, the statement next to the if statement is indented, which shows that this statement is under the scope of the if statement and will be executed if the condition is true. Otherwise, the else part will be executed.
Answered By
1 Like