Python Decision Making

Decision making in Python is carried out via ‘if’ statements, which allow a decision to be made between two or more options.

In its most basic form, an ‘if’ statement executes a group of statements if an expression evaluates to true. Its basic syntax is as follows:

if expression:
   # Statement(s) will execute if the expression is true.

The following example checks whether the values of two variables are the same and displays a message in the console if they are.

a = 10
b = 10

if a == b:
   print("a is equal to b")

In Python indentation of code within an ‘if’ statement is very important because without it an indentation error will be produced. Indentation of code is beneficial because it improves readability and maintainability.

The above can be extended to execute a statement or statements if the expression is false as follows.

if expression:
   # Statement(s) will execute if the expression is true.
else:
   # Statement(s) will execute if the expression is false.

This example adds an ‘else’ statement to the one above to output a message if ‘a’ and ‘b’ are not equal.

a = 10
b = 10

if a == b:
   print("a is equal to b")
else:
   print("a is not equal to b")

The ‘if’ statement can be further extended with the use of ‘elif’. An ‘elif’ statement is equivalent to ‘else if’ in other languages. Any number of ‘elif’ statements can be used to extend the decision making process.

if expression:
   # Statement(s) will execute if the expression is true.
elif expression:
   # Statement(s) will execute if the first expression is false
   # and the second expression is true.
else:
   # Statement(s) will execute if both the expressions are false.

Again, the above example, that compares the variables ‘a’ and ‘b’ can be extended to check if they are equal, then check if ‘a’ is greater than ‘b’ and if neither of the conditions are true, output a third message.

a = 10
b = 10

if a == b:
   print("a is equal to b")
elif a > b:
   print("a is greater than b")
else:
   print("a is less than b")

‘if’ statements can also be nested one inside another.

if expression:
   # Statement(s) will execute if the expression is true.
   if expression:
      # Statement(s) will execute if the expression is true.

Below are some examples of ‘if’ statements using the logical operators discussed in the previous section on Operators.

a = True
b = True
c = False

if a and b:

   # Display a message if 'a' and 'b' are true.
   print("Condition 1 is true")

if c or b:

   # Display a message if 'c' or 'b' are true.
   print("Condition 2 is true")

if c and b:

   # Display a message if 'c' and 'b' are true.
   print("Condition 3 is true")

else:

   # Display a message if 'c' and/or 'b' are not true.
   print("Condition 3 is not true");

if not(c and b):

   # Display a message if 'c' and/or 'b' are not true.
   print("Condition 4 is true")

The results of running the above will be as follows:

Condition 1 is true
Condition 2 is true
Condition 3 is not true
Condition 4 is true

The Ternary Operator

The ternary operator provides a means to write an ‘if’ statement in shorthand form.

true if expression else false

The expression can be the same as in a normal ‘if’ statement. If it evaluates to true then the code before the ‘if’ gets executed, otherwise the code after the ‘else’ gets executed.

The following example produces the same results as the ‘if-else’ statement above. Here the ternary operator is used as part of a ‘print’ statement, to incorporate ‘equal to’ or ‘not equal to’, into a message, depending on whether the variables are equal or not.

a = 10
b = 10

print("a is " + ("equal to" if a == b else "not equal to") + " b")