control Statements(if, if-else, if-else-if, nested if)

Simple If statement 

print("enter value of a")
a=int(input())
if a>=0:
    print("postive number")
    
Output:

enter value of a
5
postive number

enter value of a
-5

Note:
  • there is no else statement , so output is empty
  • we can not write condition in else block

======================================================================
If-else Statement

print("enter value of a")
a=int(input())
if a>=0:
    print("postive number")
else:
    print("negative number")


Output:

enter value of a
5
postive number


enter value of a
-3
negative number

======================================================================
If-else-if Statement

print("enter a value is")
a=int(input())

print("enter b value is")
b=int(input())

if a>b:
    print("a is greater than b")
elif a<b:
        print("a is smaller than b")
else:
        print("both are equal")
        
Output:

enter a value is
10
enter b value is
30
a is smaller than b

enter a value is
45
enter b value is
45
both are equal





marks = int(input("Enter the marks? "))

if marks > 85 and marks <= 100:  
   print("Congrats ! you scored grade A ...")
   
elif marks > 60 and marks <= 85:  
   print("You scored grade B + ...")
   
elif marks > 40 and marks <= 60:  
   print("You scored grade B ...")
   
elif (marks > 30 and marks <= 40):  
   print("You scored grade C ...")
   
else:  
   print("Sorry you are fail ?")  


Output:

Enter the marks? 90
Congrats ! you scored grade A ...

Enter the marks? 45
You scored grade B ...

======================================================================
Nested if



print("enter value of a:")
a=int(input())

print("enter value of b:")
b=int(input())

print("enter value of c:")
c=int(input())


if a>b:
    if a>c:
        print("a is big")
    else:
        print("c is big")
else:
    if b>c:
        print("b is big")
    else:
        print("c is big")
        


Output:

enter value of a:
10
enter value of b:
20
enter value of c:
15
b is big



enter value of a:
30
enter value of b:
45
enter value of c:
55
c is big



enter value of a:
100
enter value of b:
56
enter value of c:
67
a is big