Treating Values as Conditions


If I asked you to evaluate 35 + 2 you'd come back quickly with 37. But if I asked you to evaluate 37 as either true or false, you'd probably come back with, "Huh?" But the idea of looking at any value as either true or false is valid in Python. Any value, of any type, can be treated this way. So, 2749, 8.6, "banana", 0, and "" can each be interpreted as true or false. This may seem bizarre, but it's easy. The rules that establish true and false are simple. More importantly, interpreting values this way can make for more elegant conditions.

Introducing the Maitre D' Program

If you haven't been snubbed at a fancy, French restaurant lately, then I have just the program for you. Maitre D' welcomes you to the fine eatery and then asks you how much money you slip your host. If you give zero dollars, then you are rightly ignored. If you give some other amount, then your table is waiting. Figures 3.11 and 3.12 show off the program.

click to expand
Figure 3.11: When you don't tip the maitre d', there are no tables to be found.

click to expand
Figure 3.12: This time, my money has helped cure the maitre d' of his amnesia.

From watching the program run, you might not be impressed. This seems like something you could have already done. The difference is, there is no comparison operator used in this program. Instead, a value (the amount of money) is treated as a condition. Take a look at the code to see how it works:

 # Maitre D' # Demonstrates treating a value as a condition # Michael Dawson - 1/3/03 print "Welcome to the Chateau D' Food" print "It seems we are quite full this evening.\n" money = int(raw_input("How many dollars do you slip the Maitre D'? ")) if money:     print "Ah, I am reminded of a table. Right this way." else:     print "Please, sit. It may be a while." raw_input("\n\nPress the enter key to exit.") 

Interpreting Any Value as True or False

The new concept is demonstrated in the line:

 if money: 

Notice that money is not compared to any other value. money is the condition. When it comes to evaluating numbers, 0 is false and everything else is true. So, the above line is equivalent to

 if money != 0: 

The first version is simpler, more elegant, and more intuitive. It reads more naturally and could be translated to "if there is money".

The rules for what makes a value true or false are simple. The basic principal is this: any empty or zero value is false, everything else is true. So, 0 evaluates to false, but any other number evaluates to true. The empty string, "", is false, while any other string is true. As you can see, most every value is true. It's only the empty or zero value that's false. You'll find that testing for an empty value is a common thing to do, so this way of treating values can come up a lot in programs.

One last thing to note here is that if you enter a negative dollar amount, the maitre d' will still seat you. Remember, for numbers, only 0 is false. So, all negative numbers are true, just like positive ones.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net