It is also called conditional Statement means if the condition is true then the condition inside it will execute otherwise else part is execute.In Python Programming every else must have if but every if may have else.the syntax of if else condition is
Syntaxif expression: //statement //statement else: //statement //statementExample
x=10 if x==10: print("SmallCode!") else: print("Vicky Chaurasiya")Output
SmallCode Here x=10 and the condition is 10==10 it is absolutely correct,then the statement of if will execute.Other form of if else
It is modified form of if-else statement means if the we have multiple number of condition along with multiple output or if we have multiple number of choices then we use if-else ladder form.the syntax of if else condition is
Syntaxif expression: //statement //statement elif expression: //statement //statement else: //statement //statementExample
x=10 if x>5: print("x is less then 5") elif x==10: print("x is equal to 10"); else: print("x is not equal to 5 and not less then 5")
x is equal to 10 Here x=10 and in the first condition is (10==5) but it is not equal and then it check else if condtion and it is equal (10==10) and it is absolutely correct,then the statement of "else if" will execute.Nested if-else
It is also modified form of if-else statement means if the we have more the one condition then we use Nested and the syntax of if else condition is
Syntaxif expression: if expression: //statement1 //statement2 else: //statement3 //statement4Example
year=int(input("Please Enter any year=")) if year%4 == 0: if year%100 == 0: if year%400 == 0: print(year," is a Leap Year.") else: print(year," is not the Leap Year.") else: print(year," is a Leap Year.") else: print(year," is not the Leap Year.")Output
Please Enter any year=2020 2020 is a leap year.