Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Chapter 7. Truth, Comparison and Conditional Processing

Truth, Comparison and the if Statement. for and while Statements; The break , continue and pass Statements; The assert Statement

The elements of Python we've seen so far give us some powerful capabilities. We can write programs that implement a wide variety of requirements. State change is not always as simple as the examples we've seen in Chapter 6, Variables, Assignment and Input . When we run a script, all of the statements are executed unconditionally. Our programs can't handle alternatives or conditions. The other thing we can't do is write programs which do their processing “for all” pieces of data. For example, when we compute an average, we compute a sum for all of the values.

Python provides decision-making mechanisms similar to other programming languages. In the section called “Truth and Logic” we'll look at truth, logic and the logic operators. The exercises that follow examine some subtleties of Python's evaluation rules. In the section called “Comparisons” we'll look at the comparison operators. Then, the section called “Conditional Processing: the if Statement” describes the if statement. In the section called “The assert Statement” we'll introduce a handy diagnostic tool, the assert statement.

In the next chapter, Chapter 8, Looping , we'll look at looping constructs.

Truth and Logic

Many times the exact change in state that our program needs to make depends on a condition. A condition is a Boolean expression; an expression that is either True or False. Generally conditions are on comparisons among variables using the comparison operations.

We'll look at the essential definitions of truth, the logic operations and the comparison operations. This will allow us to build conditions.

Truth

Python represents truth and falsity in a variety of ways.

  • False. Also 0, the special value None, zero-length strings "", zero-length lists [], zero-length tuples (), empty mappings {} are all treated as False.

  • True. Anything else that is not equivalent to False.

We try to avoid depending on relatively obscure rules for determining True vs. False. We prefer to use the two explicit keywords, True and False. Note that a previous version of Python didn't have the boolean literals, and some older open-source programs will define these values.

Python provides a factory function to collapse these various forms of truth into one of the two explicit boolean objects.

bool ( object ) → boolean

Returns True when the argument object is one the values equivalent to truth, False otherwise.

Logic

Python provides three basic logic operators that work on this Boolean domain. Note that this Boolean domain, with just two values, True and False, and these three operators form a complete algebraic system, sometimes called Boolean algebra, after the mathemetician George Boole. The operators supported by Python are not , and and or . We can fully define these operators with rule statements or truth tables.

This truth table shows the evaluation of not x for both vales of x .

print "x", "not x"
print True, not True
print False, not False
x not x
True False
False True

This table shows the evaluation of x and y for all combination of True and False.

print "x", "y", "x and y"
print True, True, True and True
print True, False, True and False
print False, True, False and True
print False, False, False and False
x y x and y
True True True
True False False
False True False
False False False

An important feature of and is that it does not evaluate all of its parameters before it is applied. If the left-hand side is False or one of the equivalent values, the right-hand side is not evaluated, and the left-hand value is returned. We'll look at some examples of this later.

For now, you can try things like the following.

print False and 23
print 23 and False

This will show you that the first false value is what Python returns for and .

This table shows the evaluation of x or y for all combination of True and False.

x y x or y
True True True
True False True
False True True
False False False

Parallel with the and operator, or does not evaluate the right-hand parameter if the left-hand side is True or one of the equivalent values.

As a final note, and is a high priority operator (analogous to multiplication) and or is lower priority (analogous to addition). When evaluating expressions like a or b and c, the and operation is evaluated first, followed by the or operation.

Exercises

  1. Logic Short-Cuts. We have several versions of false: False, 0, None, '', (), [] and {}. We'll cover all of the more advanced versions of false in Part II, “Data Structures”. For each of the following, work out the value according to the truth tables and the evaluation rules. Since each truth or false value is unique, we can see which part of the expression was evaluated.

    • False and None
    • 0 and None or () and []
    • True and None or () and []
    • 0 or None and () or []
    • True or None and () or []
    • 1 or None and 'a' or 'b'

 
 
  Published under the terms of the Open Publication License Design by Interspire