if else in python | How to use if else in python | Python If Else

If Else Statement 

if else in python


Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met.

The if statement in Python is used to perform operations on the basis of condition. By using if-else statement, you can perform an operation either the condition is true or false.

There are many ways to use if statement in Python:

  1. If statement 
  2. If-else statement 
  3. Nested if Statement 


If Statement 

The single if statement in Python is used to execute the code if the condition is true. The syntax of if statement is given below.

if expression:
    statement 

The expression is true, then 'statement' will be executed, otherwise 'statement' is skipped.

Example:

a=int(input("Enter value of a:"))
if a>10:
    print ("value of a is greater than 10")
if a<10:
    print ("value of a is less than 10")

Output:

Enter value of a:12
value of a is greater than 10



If-else Statement 

The if-else statement in Python is used to execute the code if condition is true or false. The syntax of if-else statement is given below.

if expression:
    #statement-block1
else:
    #statement-block2

If the 'expression' is true, the 'statement-block1' is executed, else 'statement-block1' is skipped and 'statement-block2' is executed.

Example:

a=int(input("Enter value of a:"))
if a>10:
    print ("value of a is greater than 10")
else:
    print ("value of a is less than 10")

Output:

Enter value of a:13
value of a is greater than 10


elif statement 

The elif statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them.

If expression1:
    #statement-block1
elif expression2:
    #statement-block2
elif expression3:
    #statement-block3
else:
    #statement-block4


If the 'expression' is true, the 'statement-block1' is executed, else if ‘expression2’ is true then 'statement-block2' is executed, else if ‘expression3’ is true then 'statement-block3' is executed otherwise 'statement-block4' is executed.

Example:

a=int(input("Enter value of a:"))
b=int(input("Enter value of b"))
if a>b:
    print ("value of a is greater than b")
elif a<b:
    print ("value of a is less than b")
else:
    print ("a is equal to b" )

Ouput:

Enter value of a10
Enter value of b20
value of a is less than b



Nested if 

The syntax for a nested if statement is as follows.

if expression1:
    statement-block1
    if expression2:
        statement-block2
    else:
        statement-block3
else:
    statement-block4

Example:

a=int(input("Enter value of a:"))
if a<100:
    if a>50:
        print ("value of a is greater than 50")
    else:
        print ("value of a is less than 50")
else:
    print ("value of a is greater than 100")

Output:

Enter value of a100
value of a is greater than 100

Next Post Previous Post
No Comment
Add Comment
comment url