If else in Python and it’s Examples

0
514

Introduction To If … else In Python

What are the conditional statements?

if…else in Python, The if..else statement evaluates test expression and will execute the body of if only when the test condition is True. Conditional statement is used operate some conditions on the code. Conditions is generally show the result on True or False. When we want to apply any condition into the code we use conditional statements with the help of using comparison/conditional operators.

Example:-

  • Equal X == Y
  • Not Equal X!= Y
  • Greater Than X > Y
  • Greate Than Equal X >= Y
  • Less Than X < Y
  • Less Than Equal X <= Y

This all are given the output in boolean terms True or False.

If Else in Python Statement

Conditional statements are pass in into the if statement and loops.And the default statement is Else

Syntax:

If Pass Condition:
  ----Body
 else:
  ----Body

Example 1:

X = 50
 Y = 10
 If X > Y:                # Apply > Condition statement in if
         Print(‘X is Greater Than Y’)
Output: X is Greater Than Y

In this example, we create two variables contains numeric value X and Y then apply conditional statement in if loop to print the statement if the condition is false then there is no output of code because we do not pass the else statement.

Example 2:- Pass conditional statement in if and else also.

X = 10
 Y = 50
 If X > Y:                # Apply > Condition statement in if
           	print(‘X is Greater Than Y’)
 else:
           	print(‘X is Less Than Y’)
Output: X is Less Than Y

In this example we create two variables contain numeric value X and Y then apply conditional statement in if loop to print the statement but the condition is false so the else statement is executed.

Else If

It helps to pass one mode conditional statement in the loop, suppose the if condition is false then check the else if condition and execute the statement.

Syntax:

If Pass Condition:
  ---- print statement
 Elif Pass Condition:
  ----Print Statement
 else:
  ----Print Statement

Example 1:-

X = 10
 Y = 20
 If X > Y:
            Print(‘X is Greater than Y’)
 elif X < Y:
 print(‘X is Less Than Y’)
 else:
            print(‘X is Equal to Y’)
Output: X is Less Than Y

Because in this example if the condition is false but when it checks the else if condition then the condition is true so execute the else if statement

Single Line Code Of If Else Conditions

In python programming, the advantage is their provision to write if-else in Python conditional statements in single line.

Syntax: If Condition: Print Statement

Example:- Single Line Code Using If

X = 500
 Y = 200
 if X > Y: print("X is Greater Than Y ")

Output: X is Greater Than Y

Single line Code Using If Else

Syntax:- print statement if condition else print statement

Example:

X = 20
 Y = 200
 print("X is Greater ") if X > Y else print("Y Is Greater")

Output:- Y Is Greater

Multi else statement in single line

Syntax:- print statement if condition else print statement if condition else print statement

Example:

a = 330
 b = 330
 print("A Is Greater") if a > b else print("A is Equal To B") if a == b else print("B Is Greater")
 

Output: A is Equal To B

If Else Using Logical Operator

It is used when you want to check if the two conditions are true the go into loop otherwise print else statement, Uses Rule of Logical an Operator.

AND

True & True = True
False & True = False
True & False = False
False & False = False

OR

True | True = True
False | True = True
True | False = True
False | False = False

Example 1:-

X = 150
 Y = 55
 Z = 100
 if X > Y and X > Z:
    print("AND Operator Check BOTH of the conditions is True")

Output: AND Operator Check BOTH of the conditions is True

Example 2:-

X = 150
 Y = 55
 Z = 555
 if X > Y or X > Z:
     print("OR Operator Check one of the conditions is True")
 

Output: OR Operator Check one of the conditions is True

Nested loop

Perform loop inside the loop is called a Nested Loop.
The “First loop” will be run one time for each iteration of the “Second loop”

Syntax:

If Condition:
    If Condition:
      Print(s)
   else:
         Print(s)

Example:

x = 200

 if x > 100:
     print("Input Value Is Above Than 100 ")
     if x > 250:
         print("and also above 250")
     else:
         print("But not above 250.")
Output: Input Value Is Above Than 100 But not above 250.

Conclusion

In this blog, Cover If else conditional statement using comparison operator how to write single line code and nested loop this all is given better understanding about the if-else statement and its syntax.

LEAVE A REPLY

Please enter your comment!
Please enter your name here