In our day to day life, we need to make decisions, based on present situations. Similarly, the business problem has defined options to choose from the current information available in our hands. To tackle this kind of problem we need to make decisions in programming also.
Python has given us conditional statements for coding this kind of solutions. Different type of decision making or conditional statements are:
IF condition: In this, you have only one option to choose from, if those conditional criteria satisfy you pursue that task otherwise you won't. e.g., If the tire of your car is flat, you will take a cab. The image shows the flow of statements
#Code cond=13 if cond >12 : print("greater than 12") #OUTPUT greater than 12
if-else condition: In this, you have two option, if the condition matches you perform a task otherwise you perform a different task. e.g., On a Friday evening, if the bar has happy hours than you drink two beers, otherwise you will have only one beer.
#Code cond=10 if cond >12 : print("greater than 12") else : print("lesser than 12") #OUTPUT lesser than 12
if elif condition: In this type of conditional statement, you have a set of options to choose depending on the matching criteria. e.g., If it is hot outside, I'll switch on the air conditioner, else if it is raining outside, I'll pull up the glasses of my car, else if the weather is pleasant, I'll keep the glasses down and enjoy the weather while driving.
#Code marks=40 if marks >= 75 : print("distinction") elif marks >= 60 : print("First Division") elif marks >= 50 : print("Second Division") elif marks >= 40 : print("Third Division") else : print("Fail") #OUTPUT Third Division
Nested conditions: Python allows you to use all of the above mentioned types of decision statements inside each other. In this let us have a technical example. Assume that you have initiated a data read process and captured the data in an object. The data is about the name, age, sex and color choice. If the object is not null, then you check for the color to bucketize the entity into a set where you will have if elif ladder to make the decision based on the color of choice of that person.