Introduction

Python if statement
Syntax:
- if condition:
- statement(s)

Example:
- a=int(input("Enter 1st number : "))
- b=int(input("Enter 2nd number : "))
- if(b!=0):
- c=a/b
- print("%d / %d = %d"%(a,b,c))

If-else statements
Syntax:
- if condition:
- statement(s)
- else:
- Statement(s)

Example:
- num=int(input("Enter a number : "))
- if(num%2==0):
- print("%d is Even Number"%num)
- else:
- print("%d is Odd Number"%num)
Output:

Nested if Statements
Syntax:
- if condition-1:
- if condition-2:
- if condition-3:
- statement-1
- else:
- statement-2
- else:
- statement-3
- else:
- statements-4
When a series of decision are involved, we may have to use more then one if-else statement in the nested form.
In the above syntax:
- condition-1, condition-2, and condition-3 all are true, then statement which is written under condition-3 will execute.
- condition-1, condition-2 all are true but condition-3 is false, then statement(s) which are written under condition-3's else part will be executed.
- condition-1 is true but condition-2, is false, then it will not check for condition-3, and statement(s) which are written under condition-2's else part will be executed.
- condition-1 is false then condition-2 and condition-3 never be executed and condition-1's else part will execute.
Flow chart:

Example:
- print("Welcome to KBC")
- print()
- print("C Language is developed By:")
- print("A. Dennis Ritchie")
- print("B. Bjarne Stroustrup")
- print("C. James A. Gosling")
- print("D. Dr. E.F. Codd")
- choice=str.upper(input("Choice : "))
- if(choice=='A'):
- print("Right Answer :)")
- print()
- print()
- print("When C Language Developed :")
- print("A. 1980")
- print("B. 1982")
- print("C. 1972")
- print("D. 1990")
- choice=str.upper(input("Choice : "))
- if(choice=='C'):
- print("Correct Answer :)")
- else:
- print("Wrong Answer :(")
- else:
- print("Sorry! Worng Answer :(");

- if condition-1:
- Statement-1
- elif condition-2:
- Statement-2
- elif condition-3:
- Statement-3
- elif condition-n:
- Statement-n
- else:
- Default Statement
Flow chart:

Example:
- marks=float(input("Father: How much marks you got in exam ?\nSon : "))
- if(marks>89):
- print("Father:Excellent, Now I'll give you iPhone")
- elif(marks>79):
- print("Father:Very Good,I'll give you Laptop")
- elif(marks>69):
- print("Father:Good,I'll give you Android Phone")
- elif(marks>59):
- print("Father:Nice,I'll give you Nokia-1100")
- else:
- print("Father:Fustigate, Slap and Scold...")
Output:

Example of "Nested if" and "elif" :
In this example I am combining nested if and elif together in a simple example. Suppose we have a problem. We want to find out greatest number among 3 numbers.
Flow chart:

- print("Progam to check greatest number among three numbers")
- print("__________________________________________________________")
- a=int(input("Please Enter First number : "))
- b=int(input("Please Enter Second number : "))
- c=int(input("Please Enter Third number : "))
- if(a>b):
- if(a>c):
- print("%d is gretest number"%a)
- else:
- print("%d is gretest number"%c)
- elif(b>c):
- print("%d is gretest number"%b)
- else:
- print("%d is gretest number"%c)


Santhakumar MunuswamyPosted Oct 18, 2015, 5:23 AM
Good one
Nilesh JadavPosted Oct 7, 2015, 1:51 AM
Thank you for sharing sir
Ankit BansalPosted Oct 7, 2015, 1:22 AM
nice..thanks for sharing knowledge..