In this tutorial, we will learn about the Python if…else Statement , or its types, and how to use them with the help of examples.
Decision making in Python structures have one or more conditions to be evaluate or test by the program, along with a statement or statements that are to be execute if the condition is determine to be true, and optionally, other statements to be execute if the condition is determine to be false.
Types of Python if…else Statement
- If Statement
- If-else Statement
- Nested If Statements
- elif statement
If Statement
This statement is use to test a particular condition and if the condition is true, it executes a block of code is know If Statement.
Flow Diagram in If Statement
Syntax of If Statement
Syntax of If Statementif condition: # Statements to execute if # condition is true
The Example of If Statement
Input Value
Example of If Statementnum = int(input("enter the number?")) if num%3 == 0: print("odd number")
Let’s run the code
If-else Statement
This statement provides an else block combine with the if statement which is executes in the false case of the condition. If the condition is true, then the if-block is execute. Otherwise, the else-block is execute.
Flow Diagram in If-else Statement
Syntax of If-else Statement
Syntax of If-else Statementif (condition): # Executes this block if # condition is true else: # Executes this block if # condition is false
The Example of If-else Statement in Python
Input Value
Example of If-else Statement in Pythonage = int (input("Enter your age? ")) if age>=18: print("You are eligible to vote !!"); else: print("You are not eligible to vote !!");
Let’s run the code
Nested if Statement
This statement represents the if block within another if block. Here, the inner if block condition executes only when outer if block condition is true.
Flow Diagram of Nested if Statement
Syntax of Nested if Statement
Syntax of Nested if Statementif (condition1): # Executes when condition1 is true if (condition2): # Executes when condition2 is true # if Block is end here # if Block is end here
The Example of Nested if Statement in Python
Input Value
Example of Nested if Statement in Pythoni = 25 if (i == 25): if (i < 35): print("i is smaller than 35") if (i < 37): print("i is smaller than 37 too") else: print("i is greater than 35")
elif statement
This statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them.
Flow Diagram of elif Statement in Python
Syntax of elif Statement in Python
Syntax of elif Statement in Pythonif expression 1: # block of statements elif expression 2: # block of statements elif expression 3: # block of statements else: # block of statements
The Example of elif Statement in Python
Example of elif Statement in Pythonnumber = int(input("Enter the number?")) if number==80: print("number is equals to 85") elif number==70: print("number is equal to 79"); elif number==86: print("number is equal to 84"); else: print("number is not equal to 85, 79 or 84");
If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.
To know more about Python Wikipedia please click here .
Stay Connected Stay Safe, Thank you
0 Comments