Python Programming/Decision Control

Python, like many other computer programming languages, uses Boolean logic for its decision control. That is, the Python interpreter compares one or more values in order to decide whether to execute a piece of code or not, given the proper syntax and instructions.

Decision control is then divided into two major categories, conditional and repetition. Conditional logic simply uses the keyword if and a Boolean expression to decide whether or not to execute a code block. Repetition builds on the conditional constructs by giving us a simple method in which to repeat a block of code while a Boolean expression evaluates to true.

Boolean Expressions
Here is a little example of boolean expressions (you don't have to type it in): With the output being: 1 True 2 False 3 True 4 False 5 True 6 True 7 False 8 True 9 False What is going on? The program consists of a bunch of funny looking print statements. Each print statement prints a number and an expression. The number is to help keep track of which statement I am dealing with. Notice how each expression ends up being either True or False; these are built-in Python values.

The lines: print out True and False respectively, just as expected, since the first is true and the second is false. The third print, print (3, a == 6 and b == 7), is a little different. The operator  and means if both the statement before and the statement after are true then the whole expression is true otherwise the whole expression is false. The next line, print (4, a == 7 and b == 7), shows how if part of an  and expression is false, the whole thing is false. The behavior of  and can be summarized as follows:

Note that if the first expression is false Python does not check the second expression since it knows the whole expression is false.

The next line, print (5, not a == 7 and b == 7), uses the <tt>not</tt> operator. <tt>not</tt> just gives the opposite of the expression (The expression could be rewritten as <tt>print (5, a != 7 and b == 7)</tt>). Here's the table:

The two following lines, <tt>print (6, a == 7 or b == 7)</tt> and <tt>print (7, a == 7 or b == 6)</tt>, use the <tt> or</tt> operator. The <tt> or</tt> operator returns true if the first expression is true, or if the second expression is true or both are true. If neither are true it returns false. Here's the table:

Note that if the first expression is true Python doesn't check the second expression since it knows the whole expression is true. This works since <tt> or</tt> is true if at least one of the expressions are true. The first part is true so the second part could be either false or true, but the whole expression is still true.

The next two lines, <tt>print (8, not (a == 7 and b == 6))</tt> and <tt>print (9, not a == 7 and b == 6)</tt>, show that parentheses can be used to group expressions and force one part to be evaluated first. Notice that the parentheses changed the expression from false to true. This occurred since the parentheses forced the <tt> not</tt> to apply to the whole expression instead of just the <tt> a == 7</tt> portion.

Here is an example of using a boolean expression:

See the Lists chapter for an explanation of the slice operator, [:], occurring in.

Here is the output: First Match: Jill

This program works by continuing to check for a match <tt> while count < len(copy) and copy[count] != prev</tt>. When either <tt> count</tt> is greater than the last index of <tt> copy</tt> or a match has been found the <tt> and</tt> is no longer true so the loop exits. The <tt> if</tt> simply checks to make sure that the <tt> while</tt> exited because a match was found.

The other 'trick' of <tt> and</tt> is used in this example. If you look at the table for <tt> and</tt> notice that the third entry is "false and won't check". If <tt> count >= len(copy)</tt> (in other words <tt> count < len(copy)</tt> is false) then copy[count] is never looked at. This is because Python knows that if the first is false then they both can't be true. This is known as a short circuit and is useful if the second half of the <tt> and</tt> will cause an error if something is wrong. I used the first expression (<tt> count < len(copy)</tt>) to check and see if <tt> count</tt> was a valid index for <tt> copy</tt>. (If you don't believe me remove the matches `Jill' and `Life', check that it still works and then reverse the order of <tt> count < len(copy) and copy[count] != prev</tt> to <tt> copy[count] != prev and count < len(copy)</tt>.)

Boolean expressions can be used when you need to check two or more different things at once.

Examples
password1.py

Sample runs What is your name? Josh What is the password? Friday Welcome Josh

What is your name? Bill What is the password? Saturday I don't know you.

Exercises

 * 1) Write a program that has a user guess your name, but they only get 3 chances to do so until the program quits.

Solutions