Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

2.4 The Boolean Type

Conditions in if statements evaluate to True or False. One of the types of values in Python is called bool which is short for Boolean. George Boole was an English Mathematician who lived during the 1800s. He invented the Boolean Algebra and it is in honor of him that true and false are called Boolean values today [13].

In an if statement the condition evaluates to true or false. The Boolean value of the condition decides which branch is to be executed. The only requirement for a condition in an if statement is that it evaluates to true or false. So writing if True ... would mean that the then statements would always be executed. Writing such an if statement doesn't really make sense, but using Boolean values in if statements sometimes does.

Example 2.7 Consider a program that must decide if a value is between 0 and 1. The program below uses a Boolean expression to discover if that is the case or not.

x = i n t ( i n p u t ("Please enter a number:"))

i f x >= 0 and x <= 1:

p r i n t (x, "is between 0 and 1")

Because an if statement only requires that the condition evaluates to true or false, any expression may be used as long as the result of evaluating it is true or false. Compound Boolean expressions can be built from simple expressions by using the logical operators and, or, and not. The and of two Boolean values is true when both Boolean values are true as shown in Fig. 2.7. The or of two Boolean values is true when one or the other is true, or when both are true as depicted in Fig. 2.8. The not of a Boolean value is true when the original value was false. This is shown in Fig. 2.9. The three figures describe the truth-tables for each of the Boolean operators. A truth-table can be constructed for any compound Boolean expression. In each of the

A

B

A and B

False

False

False

False

True

False

True

False

False

True

True

True

Fig. 2.7 The and operator

A

B

A orB

False

False

False

False

True

True

True

False

True

True

True

True

Fig. 2.8 The or operator

A

not A

False

True

True

False

Fig. 2.9 The not operator

truth tables, A and B represent any Boolean expression. The tables show what the Boolean value of the expression A and B, A or B, and not A would be, given the values of A and B in the table. The and, or, and not logical operators can be strung together in all sorts of ways to produce complex Boolean expressions, but writing a program with complex Boolean expressions is generally a bad idea since it is difficult to understand the logic of complex expressions.Keeping track of whether to use and or or when not is involved in the expression is difficult and should be avoided if possible. There are at least a couple of ways that negation (i.e. the use of the not operator) can be avoided in if statements. The statement can be rewritten to test the opposite of what you first considered. Another technique is to use the guess and check pattern.

The following two examples illustrate how this can be done.

Example 2.8 Consider a club where you must be under 18 and over 15 to join. Here is a first try at a program that tells you whether you can join or not.

1 age = i n t ( i n p u t ("Please enter your age:"))

2 i f ( n o t age > 15) and ( n o t age < 18):

3 p r i n t ("You can't join")

4 e l s e :

5 p r i n t ("You can join")

Does this program do the job? In fact, as it is written here everyone can join the club. The problem is with the choice of and in the Boolean expression. It should have been or. The correct program would be written as follows. 50 2 Decision Making

1 age = i n t ( i n p u t ("Please enter your age:"))

2 i f ( n o t age > 15) o r ( n o t age < 18):

3 p r i n t ("You can't join")

4 e l s e :

5 p r i n t ("You can join")

While the program above is correct, it is still difficult to understand why it is correct. The problem is the use of negation with the or operator. A much better way to write it would be to remove the negation in the expression.

1 age = i n t ( i n p u t ("Please enter your age:"))

2 i f age > 15 and age < 18:

3 p r i n t ("You can join")

4 e l s e :

5 p r i n t ("You can't join")

Example 2.9 The guess and check pattern can be applied to Boolean values as well. If you need to decide a yes or no question, you can make a guess and then fix it if needed.

1 age = i n t ( i n p u t ("Please enter your age:"))

2 member = True

3 i f age <= 15:

4 member = False

5 i f age >= 18:

6 member = False

7 i f member:

8 p r i n t ("You can join")

9 e l s e :

10 p r i n t ("You can't join")

The technique used in Example 2.9 is especially useful when there are a number of conditions that must be checked to make sure that the yes or no answer is correct. In fact, when the exact number of conditions is unknown, this technique may be necessary. How the exact number of conditions to check can be unknown will become clearer in the next chapter.

Practice 2.5 Write a program that determines whether you can run for president.To run for president the constitution states: No Person except a naturalborn Citizen, or a Citizen of the United States, at the time of the Adoptionof this Constitution, shall be eligible to the Office of President; neither shallany Person be eligible to that Office who shall not have attained to the Age ofthirty five Years, and been fourteen Years a Resident within the United States[7]. Ask three questions of the user and use the guess and check pattern todetermine if they are eligible to run for President.

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel