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

1 Like

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.

1 Like

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.

1 Like

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

1 Like

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

1 Like

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

1 Like

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.”)

1 Like

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.

1 Like

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:

1 Like

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.")
1 Like

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.")
1 Like

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-

1 Like

In Python, indentation is significant, and it is used to define blocks of code. The correct way to write the code would be as shown in Type 2, where the indentation is consistent with the structure of the code.

pythonCopy code

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 typically four spaces, and it is crucial for defining the scope of statements within conditional structures, loops, functions, etc. Inconsistent or incorrect indentation may result in syntax errors or misinterpretation of the code by the Python interpreter.

So, in summary, Type 2 is the correct way to write the code in Python.

1 Like

Hi @athramsanthosh183
Your both the code are wrong. When you run the code you will be getting error since your indentation are wrong.
The correct code will be:

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.")

The output you will be getting:
You will get the chocolates.
Outside of conditional statements.

1 Like

Hi @venkatavas467

Please try the above code and let us know

1 Like

In Python, the order of syntax typically follows a structured format to ensure code readability and functionality. Here’s a general outline of the common syntax order in Python:

  1. Comments: These are optional, but they’re essential for explaining code logic and functionality to other developers or your future self. Comments start with # and continue until the end of the line.

  2. Imports: Import necessary modules or packages that your code relies on. This typically appears at the beginning of a script or module.

  3. Variable and Constant Declarations: Define variables and constants that will be used throughout your program.

  4. Function and Class Definitions: Define functions and classes to encapsulate reusable blocks of code and data structures, respectively.

  5. Main Program Logic: Write the main logic of your program, which may include conditional statements (if, elif, else), loops (for, while), and other control flow structures.

  6. Error Handling: Include code to handle exceptions or errors gracefully, using try, except, finally, and other related constructs.

  7. Output: Display results or information to the user, typically using print() statements.

  8. Other Operations: Any other miscellaneous operations or tasks your program needs to perform.

Here’s a simple example illustrating this order:

# This is a comment explaining the purpose of the code

# Step 1: Imports
import math

# Step 2: Variable and Constant Declarations
radius = 5
PI = math.pi

# Step 3: Function Definition
def calculate_area(radius):
    return PI * radius ** 2

# Step 4: Main Program Logic
area = calculate_area(radius)

# Step 5: Output
print("The area of the circle is:", area)
``
1 Like