Python – Decision Making

Decision-making implies identifying conditions that may arise during program execution and identifying actions to be taken in response to such conditions.

Multiple expressions are evaluated by decision structures, and the result is either TRUE or FALSE. If the outcome is TRUE or FALSE, you must decide which action to take and which statements to execute.

Decision Making Statements

There are four types of decision making statements in Python. The following tutorials detail about these statements.

  • if Statement
  • if…else Statement
  • Else if Statement
  • Nested if Statement

1.Python if statement

The if statement in Python is a conditional statement that is used to test a condition. In our Python application, we can utilize the if statement to execute conditional activities. If the supplied condition is true, the if statement is executed. In the if parentheses, we can put any acceptable phrase.

If Statement Syntax

if statement, which will evaluate whether a statement is true or false, and run code only in the case that the statement is true.

if(condition):
   statements

Example of if statement :

grade = 70
if grade >= 65:
    print("passed")

We have the variable grade in this code, and we’ve given it the integer value of 70. The if statement is then used to determine whether the variable grade is greater than or equal to 65. We tell the program to print out the string passed if it meets this condition.

output of above example :

passed

Again, if we change the grade to 50 or a < 65 number, we will receive no output.

2. Python If Else Statements

Even if an if statement evaluates to false, we are likely to want the program to do something. In the previous tutorial’s grade example, we wanted to know whether the grade was passing or failing.

Python If Else Syntax :

if condition:
   statement
else:
   statement

To do this, we will add an else statement to the grade condition above that is constructed like this:

grade = 60

if grade >= 65:
    print("Passed")
else:
    print("Failed")

Since the grade variable above has the value of 60, the if statement evaluates as false, so the program will not print out Passed. The else statement that follows tells the program to do something anyway.

When we save and run the program, we’ll receive the following output:

Failed

If we then rewrite the program to give the grade a value of 65 or higher, we will instead receive the output Passed.

3. Python Else if statement

So far, conditional statements have only had a Boolean option, with each if statement evaluating to true or false. In many circumstances, we’ll need a program that assesses more than two outcomes. We’ll do this with an else if statement, which is known in Python as elif. The elif or else if statement is similar to the if statement, except it evaluates a different condition.

Python Nested If Else Syntax :

If condition :
    statements
elif condition:
    statements
else:
    statements

Python Nested If Else Example :

grade=88
if grade >= 90:
    print("A grade")
elif grade >=80:
    print("B grade")
elif grade >=70:
    print("C grade")
elif grade >= 65:
    print("D grade")
else:
    print("Failed")

Since elif statements will evaluate in order, we can keep our statements pretty basic. This program is completing the following steps:1

The program will publish if the grade is more than 90. The program will go on to the following statement if the grade is less than 90…2

If the grade is greater than or equal to 80, the program will print B grade, if the grade is 79 or less, the program will continue to the next statement…3

If the grade is greater than or equal to 70, the program will print C grade, if the grade is 69 or less, the program will continue to the next statement…4

If the grade is greater than or equal to 65, the program will print D grade, if the grade is 64 or less, the program will continue to the next statement…5

The program will print Failed because all of the above conditions were not met.

4. Python Nested If Statement

For scenarios when we wish to check for a secondary condition if the first condition is true, we can use nested if statements. We can achieve this by nesting an if-else statement inside another if-else statement. Consider the following syntax for a nested if statement:

if expression1:
   statement(s)
   if expression2:
      statement(s)
   elif expression3:
      statement(s)
   elif expression4:
      statement(s)
   else:
      statement(s)
else:
   statement(s)

lets run the following example :

a=10
if a>=20:
    print("Condition is True")
else:
    if a>=15:
        print("Checking second value")
    else:
        print("All Conditions are false")

output of above example :

All Conditions are false.

Leave a Comment

Your email address will not be published.