Best Laptop
if-else statement in Python

if-else statement in Python

In this tutorial, we will see the use of if-else statement in Python. It is useful when we want to take decision based on some condition. For example, if I am hungry, I will go for lunch. Here I am deciding that I will go for lunch when I am hungry. To code such things, we need if-else statement.

Before directly going for Python if-else statement, let’s just revise the Python Operator priorities table and put some more operators:

PriorityOperatorType
1+, –unary
2** 
3*, /, //, % 
4+, –binary
5<, <=, >, >= 
6==, != 

How to use if-else statement in Python ?

1. Using if-else statement:

if i_am_hungry:
      go_for_lunch()
take_rest()
  • First use ‘if’ keyword
  • Then put the condition to check and end the statement with colon
  • If the condition returns True(non-zero value), then enter the loop and trigger go_for_lunch()
  • Then execute take_rest()
  • Indentation: The instructions coming under if loop must be indented by using spaces or tabs. If the if loop is having more than one instruction, then both the instruction must be having same level of indentation.
  • Here take_rest() is not indented and it is appearing at same level of if statement, hence take_rest() is not part of if loop and will be executed after if statement irrespective of the value of condition

2. if statement with multiple instructions

if movie_is_good:
      watch_movie()
      go_for_dinner()
take_rest()

Here ‘if’ branch is having two statements having same level of indentation. take_rest() is outside if loop hence it will be executed always

3. Using else statement:

var1 = 5
var2 = 10
if var1 > var2:
      print(“First variable is greater”)
else:
      print(“Second variable is greater”)

print(“Variable comparison is done”)

In this code, we first defined two variables and then comparing them. Based on the values of the variables, either if or else will be triggered. Last print statement will be triggered always.

Note: Remember that the else is associated with the if of same indentation level.

4. Nested if-else statements:

var1 = 5
var2 = 10
var3 = 20
if var2>var1:
      if var2>var3:
                print(“Second variable is the largest variable”)
      else:
                print(“”Third variable is the largest variable”)
 else:
      if var1>var3:
                print(“First variable is the largest variable”)
      else:
                print(“Third variable is the largest variable”)
 
print(“Variable comparison is done”)

Note: The if-else combination can be determined from the indentation level. Any else statement is associated with the immediately preceding if of same indentation level.

5. elif statement – elif is nothing but short form of else if. Let’s try to re-write our previous program using elif. Also, we will put more than one condition in if statement

var1 = 10
var2 = 10
var3 = 20
 
if var2>var1 && var2>var3:
print(“Second variable is the largest variable”)
elif var1>var2 && var1>var3:
      print(“”First variable is the largest variable”)
elif var3>var1 && var3>var2:
      print(“Third variable is the largest variable”)
else:
      print(“Some variables might be equal to other variable”)
 
print(“Variable comparison is done”)

Here we have used multiple elif statements. If none of those returns True, then else will be triggered.

6. if-else statement – putting instruction on the same line

if a>b: print(“a is greater”)
else: print(“b is greater”)

If the if/else of elif branch has just one statement, then we can put the statement on the same line.

Important Points about Python if-else statements:

  • else should not be used without an if statement
  • else statement is associated with the if statement of same indentation level
  • if there is more than one if statement at same indentation level of else, then the immediately preceding if with respect to the corresponding else will be associated with that else
  • else will execute only when the immediately preceding if statement of same indentation level will return False
  • else statement must be the last one in the branch
  • else statement can be avoided as well
  • You cannot have more than one else statement at the same indentation level

Lets now see some examples to improve understanding of if-else statement in Python:

7. Example 1:

x = 20
if x == 20:
print(x == 20)
if x > 7:
print(x > 7)
if x < 20:
print(x < 20)
else:
print("Execute Else")

Output:
True
True
Execute Else

Here else will be triggered because immediately preceding if of same indentation level returns False

8. Example 2:

x = 20
if x == 20:
print(x == 20)
if x > 7:
print(x > 7)
if x <= 20:
print(x <= 20)
else:
print("Execute Else")

Output:
True
True
True

Here else will not be executed because immediately preceding if returns True

9. Example 3: Try to guess the output of the below code without running it. Then verify your guess by running the code:

x=20
if x>=20:
if x>30:
print("Monday")
elif x>25:
print("Tuesday")
elif x>20:
print("Wednesday")
else:
print("Thursday")
else:
print("Sunday")

Previous Tutorial: https://itkaksha.com/python-tutorial-6-use-of-input-function/

Leave a Reply

Your email address will not be published. Required fields are marked *

Python 3.11.1 Features