if-else statement in Python

 


  The decision-making statement is often used to regulate the flow of programme execution based on the condition. If the condition is true, the block will execute; if the condition is false, the block will not execute.


Decision-making Statement






Selection statements are also referred to as decision making statements or branching statements in Python. Selection statements are used to pick a section of the programme to run based on a criteria. The following selection statements are available in Python.

We frequently encounter situations in programming where we must pick which block of code should be executed based on a condition.



if Statement

A Boolean expression is introduced by one or more statements in an if statement.



if-else statement

When the Boolean expression is FALSE, an if statement might be proceeded by an optional else statement.


Syntax: if (condition): # Executes this block if # condition is true else: # Executes this block if # condition is false



If a condition is true, the if statement will perform a block of statements; if the condition is false, the if statement will not. But what if the condition is false and we want to do something else? This is when the otherwise statement comes in. When the condition is false, we can combine the else statement with the if statement to activate a block of code.



Example

# python program to illustrate If else statement

#!/usr/bin/python

 

i = 30

if (i < 10):

    print("i is smaller than 10")

    print("i'm in if Block")

else:

    print("i is greater than 10")

    print("i'm in else Block")

print("i'm not in if and not in else Block")


Output: 

i is greater than 10

i'm in else Block

i'm not in if and not in else Block



if-elif statement



Statements nested


One if or else if statement can be inserted inside another if or else if statement (s).


Conclusion


Here , we learned about python decision making , decision making statement in python- if else.




Comments

Popular posts from this blog

Tuples in Python

Career after B.Com or Bachelor of Commerce

Decision Tree : Where it Used ?