If statement in python
If statement is an dicision making statement.It will execute the "if" statement if the condition is true.
Syntax:
if condition: #statement
Example
n = 3 if(n>0): print("Positive")
Output
Positive
If else
If else statement is an decision making statement.It will execute the "if" block if the condition is true else it will execute the "else" block.
Syntax:
if condition: #statement else: #statement
Example
n = 5 if(n>0): print("Positive") else: print("Negative")
Output
Positive
Elif
Else-if statement is an decision making statement.else-if statement has two or more block of statement.which block of statement is true It will be executed.if all block of statement is false else block will be executed.
Example
n = 5 if(n>0): print("Positive") elif(n==0): print("Zero") else: print("Negative")
Output
Positive
Nested if else
Nested if else statement is an decision making statement.It will give if statement inside the if statement.It will contain more than one condition.If the condition is true it will get inside the another if statement.If the condition is false it will execute the else block.
Syntax:
if(condition): if(condition): #statement else: #statement else: #statement
Example
n = 7 if(n>0): if(n%2==0): print("even") else: print("odd") else: print("Negative")
Output
odd