O'Reilly logo

Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython by Richard Hightower

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required