Python if else statement

Python if else statement is nothing but a simple conditional statement which is used for creating condition in program. If your condition is true than the block of if statement will execute otherwise else block of statement will execute.

Let assume if you want to eat some outside food but the condition is you will eat food which is less than 50 INR, So it is known as condition you are giving yourself for eating outside food, Similarly you can give condition to python program. So this is basically simple conditional assumption based on our life.

Python if else statements are

1. if statement

If statement is used to decide whether statement or certain block of statement will be executed or not, Means if the certain condition is true than block of statement will execute else not. Indentation help to indicate the block of if statement.

if statement syntax

if(condition):
   #excute this block of statement
#execute this block of statement

if statement Examples

if(4>3):
   print("4 is greater than 3")
print("if block is executed")

In the above if statement examples 4 is greater than 3, So block of if statement executed successfully and than next statement below the if is executed.

Output: 
>>4 is greater than 3 
>>if block is executed

Flow chart of if statement

python if statement

2. if else statement

Python if else statement simply indicate that if the block of code of if statement is true than it will execute otherwise else block of statement will execute. Indentation help to indicate the block of if and else statement.

syntax

if(condition):
   #excute this block of statement
else:
   #execute this block of statement

example

if(4>3):
   print("4 is greater than 3")
else:
   print("3 is greater than 4")

In the above example if block of statement is true so it will and execute and else will not execute.

Output: 
>>4 is greater than 3 

Flow chart of python if else statement

python if else statement

3. nested if statement

Nested If statement is simply one if condition inside another if statement. Means if condition inside another if condition.

syntax

if(condition1):
   if(condition2)
      #excute if condition2 statement
   #execute if condition1 statement
#execute other sentence

example

if(4>3):
   if(4>5):
      print("4 is greater than 3 and 5")
   print("4 is only greater than 3")

In the above example first if block statement check condition is 4 is greater than 3 true, Than it will execute second if block inside first if block and check 4 is greater than 5 false, Than it execute first if block statement.

Output: 
>>4 is only greater than 3 

4. if elif ladder

If elif ladder enables us to check multiple statement and execute the specific block of statement depending upon the true condition among them. We can use any number of elif statement in our program depending upon our needs.

syntax

if condition1:
   #excute block of statement
elif condition2:
   #excute block of statement
elif condition3:
   #excute block of statement
else:
   #execute block of statement

examples

num= int(input("Enter number?"))
if num==50:
   print("number is equal to 50")
elif num==100:
   print("number is equal to 100")
elif num==150:
   print("number is equal to 150")
else:
   print("number is not equal to 50,100 and 150")

In the above example if block of statement is true so it will and execute other wise next condition is checked and execute.

Output:

Enter number?150
>>number is equal to 150

5. short hand if statement

Whenever there is only a single statement to be executed inside the if block then shorthand if can be used.

if(9>2):print("9 is greater than 2")

Output

>>9 is greater than 2

6. short hand if else statement

This can be used to write the if-else statements in a single line where there is only one statement to be executed in both if and else block.

Syntax:

statement_when_True if condition else statement_when_False

Examples:

i = 9
print(True) if i < 15 else print(False)
Output:
>>True

Leave a Reply