2.7 Decision Making: The if Statement and Comparison Operators

A condition is a Boolean expression with the value True or False. The following determines whether 7 is greater than 4 and whether 7 is less than 4:

In [1]: 7 > 4
Out[1]: True

In [2]: 7 < 4
Out[2]: False

True and False are keywords—words that Python reserves for its language features. Using a keyword as an identifier causes a SyntaxError. True and False are each capitalized.

You’ll often create conditions using the comparison operators in the table at the top of the next page:

A Table shows comparison operators.

Operators >, <, >= and <= all have the same precedence. Operators == and != ...

Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud 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.