If Else conditions in python

 IF-ELSE CONDITION IN PYTHON

In English language ,in our daily communication we use these words very often we use the if  we are expecting something to happen and else incase if doesn't work this thing is similar in python. We use the if syntax to check a condition and we use the else condition when the if condition is not satisfied. Let us now understand them with an example program

prog:

a=100

if  a==100 :

    print("YES!!! a is 100")

    print("The above statement is generated by if condition")

print("program completed")

output:

YES!!! a is 100

The above statement is generated by if condition

program completed


So, In the above program the variable a is assigned with the value 100 and the value is checked by the if condition since ,the value of a is 100 the statement inside the if condition executes and the it goes to the next statements  the statement that are to be executed inside the if condition need to be written after giving a tab space/four spaces now let us look at one more example

prog:

a=55

if  a==100 :

    print("YES!!! a is 100")

    print("The above statement is generated by if condition")

print("program completed")

output:

program completed


Now in this example the variable a is assigned with the value 55 so the if condition verifies the variable a since a is not equal to 100 the statements inside the if condition will not be executed and it directly prints the next statements after the if conditions. Now let us see the syntax of else

prog:

a=55

if  a==100 :

    print("YES!!! a is 100")

    print("The above statement is generated by if condition")

else :

    print("The value of a is 55")

    print("The above statement is generated by else condition")

print("program completed")

output:

The value of a is 55

The above statement is generated by else condition

program completed

In this example the if condition fails so it has gone through the else condition and the statements inside the else condition gets executed and then the next statements will be executed. Without using the if condition else condition will not be executed. 

Comments