Else Statement in Python:


  • The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement.
  • We can use the else statement with if statement to execute a block of code when the condition is false

if (condition):

    # Executes this block if

    # condition is true

else:

    # Executes this block if

    # condition is false


#1: Python program to illustrate If else statement
  1.  i = 2;
  2. if i< 1:
  3.     print ("i is smaller than 1")
  4.     print ("i'm in if Block")
  5. else:
  6.     print ("i is greater than 1")
  7.     print ("i'm in else Block")
  8. print ("i'm not in if and not in else Block")

Output:
  1. I is greater than 1
  2. I'm in else Block
  3. I'm not in if and not in else Block



Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu