June 2002
Beginner to intermediate
640 pages
14h 24m
English
The if statement conditionally executes a statement's suite. One form is shown here:
if(expression):
suite
Python has to determine whether if's expression is true or false. If the expression is true, Python executes the suite; if not, it doesn't. As we discussed in Chapter 3, Python doesn't have a Boolean (true/false) type. Instead, it uses any numeric value other than zero to indicate true and uses zero, any empty sequence, and the value None to indicate false. For example, the following will print "hello" because the expression contains a value of 1.
>>> # 1 is true
>>> if(1):
... print("hello")
...
hello
Notice that the subordinate statement, print, is indented, which shows that it's part of the suite associated with ...
Read now
Unlock full access