About python syntax order

Is their any particular type to write code as like this. I mean which type to write code to run without errors. please tell below which one of type 1 or type 2.

type 1:

homework_done = “Yes”

if homework_done == “Yes”:
print (“You will get the chocolates.”)
else:
print (“You will not get anything.”)

print (“Outside of conditional statements.”)

Type 2:

homework_done = “Yes”
if homework_done == “Yes”:
print (“You will get the chocolates.”)
else:
print (“You will not get anything.”)
print (“Outside of conditional statements.”)

In python, indentation is everything, it is used to indicate the block of code. The gaps between lines means nothing but type 1 is more readable. You can type the code as

homework_done = “Yes”
if(homework_done==“Yes”):
print(“You will get choclates.”)
else:
print(“You will not get anything.”)
print(“Outside of conditional statements.”)

Remember the content inside of conditionals must be at a indent more than the indent of the primary if/else statement. The indent is usually a tab, can be 2 or 4 spaces depending upon the ide you are using.
Hope you understood.

1 Like

python requires proper indentation as it does not have any special indications as in other languages to show end of statement. The indents help in understanding which block the code belongs to.
In if…else block indentation is a must for error free execution which is 4 tab spaces
syntax for if…else :
if (condition):
‘’'‘statement
else:
‘’’'statement

Gaps between the lines matters nothing in Python, the thing that actually matters is INDENTATION.
Unlike other programming languages that use braces { } to define a block of code, Python uses indentation to indicate a block of code.( refer to Indentation in Python - GeeksforGeeks).

a.)
else:
print (“You will not get anything.”)

print (“Outside of conditional statements.”)

b.)
else:
print (“You will not get anything.”)
print (“Outside of conditional statements.”)

c.)
else:
print (“You will not get anything.”)
print (“Outside of conditional statements.”)

d.)
else:
print (“You will not get anything.”)
print (“Outside of conditional statements.”)

So, both (a) and (b) will give you what you are looking for, but (c) will print the statements if ‘else’ case condition satisfies, and (d) will print the both statements irrespective the condition of ‘else’ case.

I hope you got it. If not, feel free to give a reply.

Indention is a very important part of python programming.Indentation refers to the spaces at the beginning of a code line.Type1 is more preferrable .You can write your code in this manner.

homework_done = “Yes”
if(homework_done==“Yes”):
print(“You will get choclates.”)
else:
print(“You will not get anything.”)
print(“Outside of conditional statements.”)

Content that is there inside the conditionals must be indented properly.They allow you to make decisions based on the values of variables or the result of comparisons.You can use Tab button also to do indentation properly.

homework_done = “Yes”

if(homework_done==“Yes”):
print(“You will get choclates.”)

else:
print(“You will not get anything.”)

print(“Outside of conditional statements.”)

this way is easily understandable and the indentation is very important,not giving enough spacing gives errors.

the indentation is no showing here.understanding the indentation solves most of your problems

Indentation is everything in python. If you dont indent your code then it would keep showing errors because the compiler would never be able to understand what we are trying to do. whenever you write a block of code, put a colon after the main keyword and put your block of code underneath it after leaving some space. Use the Tab key to do it accurately. An example-

i = 0
while i < 10 :
print(i)
if ( i == 3) :
break
i+= 1

So here in this code the output would be 0 1 2 3
Here you can clearly see that i have given indentation after while and if using a colon for writing this program, without these additional spaces this program wont run

In Python, Indentation defines the blocks of statements or code. Indentation is the number of spaces or whitespace in the code

Python uses indentation to indicate a block of code. Indentation refers to the spaces at the beginning of a code line. Python will give you an error if you skip the indentation. Where as, in other programming languages the indentation in code is for readability only.

The code can be written as:

homework_done = “Yes”
if homework_done == “Yes”:
print (“You will get the chocolates.”)
else:
print (“You will not get anything.”)
print (“Outside of conditional statements.”)

Hi.

Python does not interpret spaces between lines as a block of statements. It uses indentation- which refers to the amount of space at the beginning of a code line. So, a set of statements with the same indentation is considered to be a block of code.

However, of the two options, Type 1 seems more readable as compared to the other, as it provides more demarcation between the if-else blocks of code. Note that both of the codes written above would give all three statements with the print command as output, simply because Python does not identify spaces as a method of defining a block of statements.

Hi athramsanthosh183,
Here is the correct code with required corrections in the indentation in your code.:
Here _ before print is to show the indendation, remove them when running the code and put blank space instead of it.

homework_done = “No” #Varible to check Condition
#Start of Loop
if homework_done == “Yes”:
_print(“You will get the chocolates.”) # True Result
else:
_print(“You will not get anything.”) # False Result
print(
“Outside of conditional statements.”) # To show that code is out of loop.

Answer to your second question is that the type of code should be such that:
1.) Code is easy to understand by us as well as other developers who work on code.
2.) Varible names must be chosen such that they are identified clearly.
3.) Use Comments where ever required to make the code understandable. As i have done in your code as an example.
4.) Use Proper line break, no unnecessary blank lines.
Rest you will automatically understand as much as you work on it.

Best of Luck :slightly_smiling_face:

It is all about indentation in python.
Here is the correct python cod with proper indentation:

homework_done = "Yes"

if homework_done == "Yes":
       print ("You will get the chocolates.")
else:
       print ("You will not get anything.")

print ("Outside of conditional statements.")

It requires proper indentation after condition examples given below hope you got this.
Exaples:

homework_done ="yes"
if homework_done== "yes":
  print("You will get the chocolates.") 
else:
  print ("You will not get anything.")

print("Outside of conditional statements.")

In python, indentation is very important.
Indentation means the spaces at the beginning of code line. It makes the code more readable and understandable. You can write the code as-