Python for Everybody - Conditional Execution

I couldn’t understand ındentation clearly, why indentation matters in phyton ?

Hi there and welcome to our community!

Whereas in JavaScript, for instance, indentation/formatting is a principle applied to ensure that your code is easy to read, in Python it is actually syntactically vital.

Improper formatting/indentation in JavaScript does not affect the efficacy of the code, as long as the code itself is syntactically correct.

However, in Python, if your code is not correctly indented, it will not work as intended or it will throw an indentation error.

The purpose of indentation in Python is to tell the Python interpreter which statements belong to which blocks of code.

Some examples are given in the link below:

When you indent a line of code, you’re telling Python that this line is a child of the line above it and should only be executed when the parent line dictates. For instance, in an ‘if’ statement scenario, the indented code beneath it will only run if the statement’s condition is true. If it’s not indented, Python won’t know it’s part of the ‘if’ statement, and it’ll be executed independently.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.