Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - IF...ELSE


'If...Else' is somewhat similar to Decision Making.


Everyone has to make some kind of Decision in everyday life. Be it choosing a shirt or going to office by Bus or Cab.


Even in case of Python, it has to make Decision.


And to make a Decision, 'If...Else' is used by Python.


There are three Decision Making statements used by Python :


  1. if
  2. else
  3. elif

Let us see them in detail.


The 'if'statement for Decision Making


Let us understand 'if' statement with the below example:


Say, you have a friend, who cannot remember anything. Now, you ask him to go to the market and get 1 kg Apple. You also tell him, if the price of Apple is less than 150 bucks, only then he should buy the Apple.


And since your friend has a weak memory. He takes out his notebook and writes :


"If the price of Apple is less than 150 then buy the apples"


Now, if your friend has to write the same thing in Python. How would he write it?


Let us see with the below example.


Example :


price = 120
if price < 150:
    print("Buy the apples")
	

Output :



  Buy the apples

So, your friend writes the same thing in Python for the same statement, "If the price of Apple is less than 150 then buy the apples".


if price < 150:
    print("Buy the apples")
	

Quite Simple ! Right ?


It works in two steps :

  1. He goes to the market and asks the shopkeeper, "What is the price of Apples?".

    Say the shopkeeper says, "The price is 120".

    He notes the same in a 'price' variable.

    price = 120


    Python If Else1
  2. Then he checks the 'if' statement.

    if price < 150:
        print("Buy the apples")


    And finds that he can buy the apples as 'price' is 120 which is less than 150.

Now, that we have seen the 'if' statement. Let us look at the syntax of 'if' statement in detail.


if price < 150:
	print("Buy the apples")

The above 'if' statement begins with a 'if', followed by the actual condition (i.e. price < 150) then with a colon ':'.


if price < 150:

Now, if the above condition (i.e. price < 150) is 'true'. The 'print(...)' statement is executed.


print("Buy the apples")

There can be lot of statements inside the 'if' statements. What I meant is, there can be a lot of print statements.


if price < 150:
    print("Buy the apples")	
    print("The apples should be of good quality")
    print("Take the apples in a carry bag")

But there is something to note. i.e. The 'if' statement doesn't have any 'end' statement. Then how does Python comes to know, all the above 'print(...)' statements belongs to the 'if' block?


To determine that Python uses something called as 'Indentation'. i.e. The statements inside the 'if' block should have at least one space in fron of it.


Just for an example, the below code is incorrect and ends up with an error.


Example :


price = 120
if price < 150:
    print("Buy the apples")

Output :



  print("Buy the apples")
    ^
  IndentationError: expected an indented block

If we see the above code, it ended with the error,


IndentationError: expected an indented block

And points to the 'print' statement.


print("Buy the apples")
  ^

It meant that the 'print(...)' statement should have 'Indentation'(i.e. At least one space in front of it).


if price < 150:
    print("Buy the apples")

The above 'if' statement can be corrected by using just one space or 'Indentation'.


if price < 150:
    print("Buy the apples")

And the above code works fine.


So, in the above example we have seen your friend is happy because the price of apple is less than 150.


But what if the price of apple is more than 150. Say, the price of apple is 180. And he doesn't know what to do.


Example :


price = 180
if price < 150:
    print("Buy the apples")

Output :



And there is no output. Because the price of apple is 180 which is more than 150.


Python If Else

So, your friend comes back to you and you tell him to use 'else' statement with 'if'.


The 'else' statement for Decision Making


So, you tell your friend,


"If the price of Apple is less than 150 then buy the apples else do not buy the apples"


So, he writes the same thing in Python using 'else' statement.


Example :


price = 180
if price < 150:
    print("Buy the apples")
else:
    print("Do not buy apples")

Output :



  print("Buy the apples")

So, to implement the line,


"If the price of Apple is less than 150 then buy the apples else do not buy the apples".


We have used the 'else' statement along with 'if'.


if price < 150:
    print("Buy the apples")
else:
    print("Do not buy apples")

The syntax of 'else' statement is almost same as 'if'. Else statement uses the 'else' keyword followed by colon ':'.


else:

And the statements inside 'else' also uses 'Indentation'.


else:
    print("Do not buy apples")

Now, your friend is happy. But you figured out that the shopkeeper is selling two types of apples for a fixed price.


1st kind of apple is 100 Bucks
2nd kind of apple is 200 Bucks.

And you give the new instruction to your friend.


The 'elif' statement for Decision Making


So, this time you tell your friend,


"If the price of Apple is 100 then is first kind of apple. Buy them else check if the price of apple is 200. Then it's second kind of apple. Buy them. It neither one is present then don't buy any apple".

And he writes the same in Python.


Example :


price = 200
if price == 100:
    print("Buy 1st kind of apple")
elif price == 200:
    print("Buy 2nd kind of apple")
else:
    print("Do not buy apples")

Output :



  Buy 2nd kind of apple

So, in the else part, if we need to provide a condition (i.e. price == 200).


Python If Else

We need to use 'elif'.


elif price == 200:
    print("Buy 2nd kind of apple")

And the syntax of 'elif' is just like 'if' and 'else' statement.


Program to find the greatest of two numbers


Say, you need to write a program to find the greatest of two numbers. And the input needs to be taken from the user.


Example :


first_num = int(input("Enter the first number : "))
second_num = int(input("Enter the second number : "))
if first_num > second_num:
    print("The first number is greater than the second number.")
elif first_num < second_num:
    print("The second number is greater than the first number")
else:
    print("The first number and the second number are equal")




So, the output will be based on the numbers entered by the user.

  1. In the first line, we have taken the input from the user using the 'input(...)' method. And since the number the user is going to be a number. We have used 'int(...)' to cast the value to an integer type.

    first_num = int(input("Enter the first number : "))


    Let us say the user had entered '5'. So, 'first_num' contains '5'.

    Python If Else
  2. Then in the second line, we follow the same process to take the input from the user for the second number.

    second_num = int(input("Enter the second number : "))


    Say, the user entered '8'. And, 'first_num' contains '5'.

    Python If Else
  3. In the third line we check, if the 'first_num' is greater than the 'second_num'.

    if first_num > second_num:
        print("The first number is greater than the second number.")


    In this case the 'first_num' is '5', which is less than the 'second_num' '8'.

    So, we come to the 'elif' part.
  4. In the 'elif' we check, if the 'first_num' is less than the 'second_num'.

    elif first_num < second_num:
        print("The second number is greater than the first number")


    And the 'elif' condition (i.e. first_num < second_num) matches as the 'first_num' is '5', which is less than the 'second_num' '8'.

    So, the print statement inside the 'elif' block is executed. And

    The second number is greater than the first number


    Is printed on the screen.

Just note that if both the conditions of 'if' and 'elif' is not satisfied, then we come to the 'else' part.


else:
    print("The first number and the second number are equal")

Which means both the values are equal.