The if Statement

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 ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.