COMP 200 Elements of Computer Science &
COMP 130 Elements of Algorithms and Computation
Spring 2012

Conditionals & Logic “Finger Exercises”

if Statement

We'll introduce the syntax of an if statement by a series of examples.

To choose the larger of two values, we can define the following function.

Similarly, to choose the largest of three values, we can define the following.

Try these two functions on some sample inputs.

We could continue defining max functions in a similar vein, but Python gives us an easier option, a built-in function max which allows any number of arguments.

When doing some operations, it is useful to first check for a possible error condition. For example,

Change y to zero and run this again.

In the above example, note that we compare equality with == (double equals), not = (single equals). We've already been using the single equals sign in the course, as its purpose is to assign a value to a variable. Do not confuse these two.

Alternately, if we don't want an error message:

Comparison Operators

The previous examples illustrate several ways of comparing two values. These return one of two possible truth values, True and False, also known as Boolean values.

For example, the following illustrate the most common comparisons. For each one, guess what the result is, then type it in to see if you were right.

Once again, we compare equality with two equals signs (==), not one (=).

There are also >= (greater than or equal) and <= (less than or equal) which work as expected.

Booleans are just another kind of value that you can assign to variables and pass to functions, as in the following.

Logic

You can combine comparisons with the operators and, or, and not, as in the following examples:

Most of the above parentheses are not necessary, but we recommend including them, rather than depending on operator precedence. Try typing the examples in fewer parentheses to see which expressions still have the same results.

Additional optional readings about Python conditionals

Combining conditionals with loops

Conditionals are often used within loops. We saw one example in the modified predator-prey code. Often, conditionals are used to check if the loop should stop early. Here are two similar functions for searching a list for a specific item. One merely prints the results, while the other returns True or False, indicating whether the item was found.

Searching a list is so common that Python provides a built-in mechanism for it. Try each of the following.

For another example, let's return to a previous example of computing the product of a list of numbers. Our previous code looked like the following.

While that works just fine, imagine that your list is really long. If there is just a single zero in the list, the product will be zero, and it could be a waste of time to multiply all those numbers. Instead, we could just stop when we found a zero. (We typically want to pick the fastest algorithm to accomplish our goal. Not only does it save the computer time, but it saves us time, as we wait for the answer. We'll talk more about this later.)

The following is one way to accomplish this idea. This function also shows us a count of how many numbers it looked at, so we can see the difference.

Both return and break provide ways to exit from some code. To exit from a function, use return. To exit from a loop, use break.