In this post, we are going to understand if-else Elif nested if in Python with examples
What are Python if-else statements
In programming, language code executes in sequential order one by one. What if we need to execute the code block after evaluating some expression and execute the code block on the basis of the evaluation result of the expression. All this could be performed using the control flow statement.
Types of Python if-else statement
Note: In Python Non-null or Non-Zero values are considered as True, Zero or Null values consider as False.
1. What is Python if Statement
if statement is a basic decision control statement. It uses when we need to execute some code statement only when the condition is true, Else nothing is performed.
if a statement evaluates an expression. if the evaluation of the expression result is TRUE. “code statement ” will be executed otherwise nothing will be executed.
if expression: ..code statement
- Expression: This expression returns TRUE or False. e.g if num>20
- Code body: code body will execute if the condition is true.
Example Python if statement
num1 = 10 num2 = 20 if num1%2==0 : print('num is even number') if num2>num1 : print('num2 is greater than num1') if num1<num2: print('num2 is less than num1')
num is even number num2 is greater than num1 num2 is less than num1
2. Python if..else statement
“If” statement executes some code block if the expression is evaluated to true, else statement executes code that is in “else” block if the expression is evaluated to false.
if expression: code body of if else: code body of else
The expression for if is evaluated to TRUE then “code body of if ” will execute, on False then “code body of else” will execute.
Python if-else Statement with Example
num1 = 10 num2 = 20 if num1%2==0 : print('num is an even number') else: print('num1 is not an even number') if num2<num1 : print('num2 is less than num1') else: print('num2 is greater than num1')
num is an even number num2 is greater than num1
3. Python if……Elif…….else Statement
In programming languages like C#, Java, “else if” is indicated by the else -if statement, In python if-else, is indicated by the Elif keyword.
Elif stands for else-if in Python. It is used when needed to evaluate multiple expressions. In this example, we only have one if-else block but we can have multiple Elif blocks.
Python if…elif…….else statement syntax
if expression: code for if block elif expression: code for elif block else: code block else block
The expression for if is evaluated to False then elif expression is evaluated then next elif expression is evaluated and so on if all condition is False then else code block will be executed.
Python if…elif…….else statement with example
x = 10 y = 20 if y<x : print('y is less than x') elif x>y: print('x is greater than y') elif x==11: print('x is 11') else: print('none of condition is true')
none of condition is true
4. Python Nested If statement
Whenever in code, we have if statement inside another if statement is called nested if statement.
Python Nested if statement syntax
if expression: if expression code for inside if-block go here else: code for inside if-block go here else: code for else-block
Python Nested if statement with example
x = 10 y = 20 if y<x : print('y is less than x') if x==10: print('x value is 10') else : print('x is value not 10') else: print('y is greater than x')
y is greater than x
In this post, We have understood if-else Elif nested if in Python with examples. We hope you find this useful and use any of them as per requirement.