Skip to content

Making Decisions

Everything so far has been limited. So far, your programs are only able to do the same thing on all data and you cannot do anything that depends on what the data is. Now, let’s figure out a way to make code behave differently based on decisions. To do so we want to find a way to make comparisons, which will give you a way to test and ultimately allow your program to decide what to do.

Comparison Operators

Python provides several operators for making comparisons:

Symbol Operator
< less than
> greater than
<= less than or equal to
== equal to
!= not equal to

All of these operators act on numbers, for example: <number> <operator> <number>. The output of a comparison is a boolean i.e. True or False. Here are some examples:

print(2 < 3) # -> True
print(21 < 3) # -> False

You can also make comparisons using expressions:

print(7 * 3 < 21) # -> False
print(7 * 3 != 21) # -> False

Note the equality is done using two equals signs (double ==), not a single =:

print(7 * 3 == 21) # -> True

Quiz 2.11

Why is the equality comparison done using == instead of =?

  • A) Because = means approximately equal.
  • B) Because not equal uses two characters !=.
  • C) Because Guido (van Rossum, the creator of Python) really likes = signs.
  • D) Because = means assignment.
  • E) It doesn’t matter, we can use either == or =.
Answer
  • A) Because = means approximately equal.

If Statements

An if statement provides a way to control what code executes based on the result of a test expression. Here is the grammar of the if statement:

if <test-expression>:
  <block>  # (1)
  1. <block> runs only if the value of the <test-expression> is True

In this statement, the code in the <block> runs only if the value of the test expression is True. Similar to procedures, the end of the if statement block is determined by the indentation. Here is an example where we use an if statement to define a procedure that returns the absolute value of its input:

def absolute(num):
  if num < 0:
    num = -num
  return num

Quiz 2.12

Define a procedure, bigger, that takes in two numbers as inputs, and outputs the greater of the two inputs.

bigger(2, 7) # -> 7
bigger(3, 2) # -> 3
bigger(3, 3) # -> 3
Answer
def bigger(a, b):
  if a > b:
    return a
  return b

Else Expressions

You can use an else clause in addition to an if statement to provide alternative code that will execute when the test expression is false. The test expression determines whether to execute the block inside the if statement or the block inside the else statement:

if <test-expression>:
  <block>
else:
  <block>

Using else, we can define bigger in a more symmetrical way:

def bigger(a, b):
  if a > b:
    return a
  else:
    return b

Quiz 2.13

Define a procedure, is_friend, that takes a string as its input, and outputs a Boolean indicating if the input string is the name of a friend. Assume I am friends with everyone whose name starts with D and no one else.

print(is_friend('Diane')) # -> True
print(is_friend('Fred')) # -> False
Answer
def is_friend(name):
  if name[0] == 'D'
    return True
  else:
    return False

There is no real need to use if statement here, since we can just return the result of the comparison directly:

def is_friend(name):
  return name [0] == 'D'

Quiz 2.14

Define a procedure, is_friend, that takes a string as its input, and outputs a Boolean indicating if the input string is the name of a friend. Assume I am friends with everyone whose name starts with either D or N, but no one else.

print(is_friend('Diane')) # -> True
print(is_friend('Ned')) # -> True
Answer
def is_friend(name):
  if name[0] == 'D':
    return True
  if name[0] == 'N':
    return True
  return False

Another way to define this would be to use an else clause:

def is_friend(name):
  if name[0] == 'D':
    return True
  else:
    if name[0] == 'N':
      return True
    else:
      return False

Note how the inner if statement is intended inside the else clause. A third way of writing this would be to use an or expression, which we will describe next:

def is_friend(name):
  if name[0] == 'D' or name[0] == 'N'

Or Expressions

An or expression gives the logical or (disjunction) of two operands. If the first expression evaluates to True, the value is True and the second expression is not evaluated. If the value of the first expression evaluates to False then the value of the or is the value of the second expression.

<Expression> or <Expression>

Here are a few examples:

print(True or False) # True
print(False or True) # True
print(True or True) # True
print(False or False) # False

An important difference between an or expression and other operators is that an or expression does not necessarily evaluate both of its operand expressions. For example:

print(True or this_is_an_error)  # -> True

Even though this_is_an_error would produce an error because the variable is not defined, the or expression does not produce an error! This is because the second operand expression of an or is only evaluated if the first expression evaluates to False. When the first operand expression evaluates to True, the output of the or expression must be True regardless of the value of the second operand expression. The Python rules of evaluation require that the second operand expression is not even evaluated in cases where the value of the first operand is True.

Quiz 2.15

Define a procedure, biggest, that takes three numbers as inputs, and outputs the greatest of the three numbers.

biggest(6, 2, 3) # -> 6
biggest(6, 2, 7) # -> 7
biggest(6, 9, 3) # -> 9
Answer
def biggest (a, b, c):
  if a > b:
    if a > c:
      return a
    else:
      return c
  else: # b >= a
    if b > c:
      return b
    else: # c >=b >= a
  return c

Another way to answer this that would be shorter and simpler is to use the bigger procedure we defined earlier:

def bigger (a, b):
  if a > b:
    return a
  else:
    return b

We can define biggest by composing two calls to bigger.

image

The code below is a much shorter way of defining biggest, taking advantage of the earlier definition of bigger:

def biggest(a, b, c):
  return bigger(bigger(a, b), c)

An even simpler way to define bigger would be to use Python’s built-in max operator, which works on any number of inputs and outputs the maximum value. We could then define:

def biggest(a, b, c):
  return max(a, b, c)

Of course, if you already knew about the built-in max operator, there would be no need to define biggest at all. The point is you know enough now to define it yourself!

You have now learnt about arithmetic comparisons, procedures, and if statements. You now know enough (in theory) to write every possible computer program.

Back to top