Decision-making In Python

Decision-making In Python

DECISION MAKING

Have you ever been in a situation where you have to make a decision in life?, yes, there are situations like that in Python. In general, we make a decision after checking a certain condition, if the condition is okay, then we go ahead and do something. In this post, I will be sharing how to make decisions in Python. Let's dive in. Apparently, we have been dealing with decision-making in our everyday life such as going to bed, getting our meals prepared and so on. Let's see an example of how we make a decision in real life.

Take for instance, you need to go out today and it all depends on two conditions, which is; checking the weather condition if it's sunny or cloudy; if it's cloudy, you take an umbrella with you if not pick up your sunglass (i.e the weather is sunny)

01.jpg

As you can see from the image above, I needed to go out but I had to check for some conditions before stepping out, that's exactly how to make decisions in Python.

IF-ELSE STATEMENT

In Python, we make decisions using the decision-making statement, which is the "if" and "else" statement. If the condition is true, the code(s) in the if statement block is been executed and if the condition is false, the code(s) in the else statement block is been executed. Let's see an example.

02.jpg

If we run the code above, we see that the 'if ' statement block of code gets executed because the variable 'day' has the value 'Sunday' assigned to it. If the value 'Monday' or any other day of the week was to be assigned to the variable 'day', then the 'else' statement block of code gets executed

CHECKING FOR MULTIPLE CONDITIONS (ELIF STATEMENT)

When we want to check for multiple conditions(more than just 2 conditions), how do we go about it? In the example above, we checked for just two conditions: if the day is Sunday and if the day is not Sunday (that means the else block of code will execute when the day is either Monday, Tuesday, Wednesday, Thursday, Friday or Saturday). But what if we want to check the condition for the rest of the days, that's when we introduce the 'else if ' statement. In Python, it's called 'elif ', it is used to check if a next condition is True when the 'if ' statement's condition failed to be True. If the elif statement returned False, it moves to the next block of code to check the condition as well. Let's see an example.

03.jpg

From the lines of code above, Python keeps checking which statement's condition will return True, then executes the statement's block of code. When it gets to elif statement where day == "Wednesday", Python executes the block of code and ignores the rest: "Today is Wednesday" gets printed out on the console.

NESTED IF STATEMENTS

Nested if statement is simply using an if-else statement inside an if or elif block of code. We want to check a particular condition inside an if or elif statement once it returns True. Once it returns True, the if else statement inside the block gets executed. Let's see an example below.

04.jpg

When we run the above code, we get the result from the image below

05.jpg

Here we have this output because the elif statement for checking if the fruit was apple returned True, so the nested statement inside the elif statement's block of code gets executed.

Note:

  • The colon ( : ) after each statement is important as it differentiates the block of code of each statement by indenting each block.

  • The lines of code from the first image are never a way of writing a Python if else statement, it's just an illustration to explain what decision-making is in the real world.