next up previous contents
Next: if statement Up: Programming Previous: Indentation   Contents


Truth, Falsehood and Logical Operators

When you write a program and need to execute statements only under certain conditions, the if statement (Section 6.4) can be used; similarly, when you want to repeatedly execute commands until some condition becomes true or false, the while statement (Section 6.7) can be used. But before looking at the mechanics of these statements, it's important to understand python's ideas of what is true and false.

In python, numeric values are false if they are equal to zero, and true otherwise; sequence objects (like strings and lists) are false if they contain no elements, and are true otherwise. Similarly, a dictionary is false if it has no keys or values and is true otherwise. Finally, the special python value None also evaluates to false. It should be noted that, while assignment statements do evaluate to a value, they can not be used as tests in if or while loops. Because of this, it is very common in python to use a so-called ``infinite'' loop, where the argument of a while clause is set to 1, and break statements are used to insure that the loop will be properly terminated. (See Section 6.7 for details)

More commonly, however, logical expressions are created using binary comparison operators. These operators return 0 if the test they represent is false, and 1 if the test is true. They are summarized in Table 6.1.


Table 6.1: Comparison Operators
Operator Tests for Operator Tests for
== Equality != Non-equality
> Greater than < Less than
>= Greater than or equal <= Less than or equal
in Membership in sequence is Equivalence
not in Lack of membership not is Non-equivalence


You can combine logical expressions with the keywords and or or. When you combine two logical expressions with an and, both of the statements being combined must be true for the overall expression to be considered true. When you use an or to combine two statements, the overall statement is true if either of the statements being combined are true. You can use as many ands and ors as you need to create expressions to test for complex conditions, and you can freely use parentheses to make sure that python understands exactly what you mean.

One subtlety of the logical expressions has to do with what are often called side effects. For reasons of efficiency, python will only evaluate a logical expression (from left to right) until it can determine whether or not the expression is true. What this means is that if you combine two logical expressions with an and, python will never even evaluate the second expression if the first expression is false, because, once the first expression is false, the value of the second expression doesn't matter -- overall, the combined expression must be false. Similarly, if you combine two logical expressions with an or, then if the first expression is true, the second expression isn't evaluated, since the combined expression must be true regardless of the second expression's value. This can lead to strange behavior of your programs if you're not prepared for it.

Suppose we have a character string and we want to count the number of words in the string, but only if the length of the string is greater than 5. In the code that follows, I'm purposely making an error. I'm refering to the split function of the string module without properly importing the module:

>>> str = 'one'
>>> if len(str) > 5 and len(split(str,' ')) > 3:
...     print 'too many words'
...
When I run this program, I don't see any error, even though I purposely refered to the split function which had not been properly imported. If the length of the string is greater than 5 however, the second part of the logical expression must be evaluated, and python informs us of the error:
>>> str = 'one two three four'
>>> if len(str) > 5 and len(split(str,' ')) > 3:
...     print 'too many words'
...
Traceback (innermost last):
  File "<stdin>", line 1, in ?
NameError: split
The moral of this example is that it's important to understand python's logic when it evaluates logical expressions, and to realize that not all of the code in your logical expressions will actually be executed every time that it's encountered.


next up previous contents
Next: if statement Up: Programming Previous: Indentation   Contents
Phil Spector 2003-11-12