Python program indentation


#1

What is the importance of identation in python??


#2

It basically makes the compiler aware of which block or statement the given piece of code is under. For example there might be a for loop, and within that loop there might be another loop or if-else statements. So if we indent the code, the compiler will understand which block the code is under. It’s just the way python is. In programming languages like C, indentation is optional, but recommended to ensure readability.


#3

Indentation is very important in python…not only it makes the code easily readable but it’s necessary in python otherwise you’ll get Indentation error. Indentation actually conveys the program structure. Different languages have different techniques for indentation for example in C / C++ a block of code is represented by curly braces
whereas in python indentation is uniform…
a example to show the importance of indentation is shown below:

x = int(input(“Input a number”))
if x%2==0:

   print("number is divisible by 2")

else:

   print("number is not divisible by 2")

this indentation is really important…Programming is just experimenting…so try to change the indentation and write without proper indentation, automatically you will get error…

Hope it helps…!!:grinning:


#4

Hello

Basically, when you write a program we need to define its structure on how the code should be executed when its run. Each language has its own way of defining structure for different blocks of code, for example in C++ we use curly braces ( { … } ) to define structure of its components, Similarly in python we use indentations to define each components body and structure and that’s a rule. we can’t use brackets of any type to define structure in python.

( Although you can use indentation in C++ but thats majorly because to increase the readability of the code, and for easy navigation to particular part of code. )

Just for getting a small idea about indentations for each block of code that we write in python, refer to this image below :

inde

Hope this helps! :+1:


#5

Just to add it bit more, for indentation in python 4 spaces is the ideal one.It’s not like it is mandatory but ideal.

Hope it helps!!:grinning:


#6

Indentation is a way in python programming to write codes. For eg. In C/c++ we put curly brackets “{ }” and between them we write our code or loop statements. In python through indentation compiler understands the code’s start and end point. Also it makes the reading the code easy


#7

Indentation is considered good-practice to improve readability of any language. So from the reader’s point of view indentation makes the code easier to read and understand.

Now, most languages make indentation purely a matter of style and they use block markers (such as keywords or parentheses) to tell the compiler(or interpreter) where a block of code starts and stops.I thinks this clears your doubt :slightly_smiling_face:


#8

Hello priyank,
In c,c++ like programming languages we use braces such as {,} to differentiate between blocks.
Instead in python ,we use indentation to differentiate between blocks.
so, that the compiler can understand it .