7.2. Conditions and Boolean expressions

The condition in the if statement has to be a boolean expression.

Conditions or Boolean expressions

Conditional or boolean expressions are expressions that are either true or false.

Here are some examples of simple boolean expressions or conditions:

>>> 1 < 0
>>> 1 > 0
>>> 1 == 0
>>> 'n' in 'ATGCGTAnAGTA'
>>> 'A' > 'C'
>>> 'AG' < 'AA'
>>> 'AG' != 'AC'
>>> len('ATGACGA') >= 10
In Python the value true is represented by 1 and the value false by 0.

Table 7.1 lists all boolean operators and their action on strings and numbers.

Table 7.1. Boolean operators

OperatorAction on stringsAction on numbers
<, <=, >=, >alphabetically sorted, lower/lower or equal/greater or equal/greater thanlower/lower or equal/greater or equal/greater than
==, !=, is, is not[a]identityidentity
in, not inmembership-

[a] See (Section 10.2) for a detailed explanation.


Do not confuse the assignment sign = with the logical operator ==. The second one is used to compare two things and check their equality whereas the first one is used to bound values to variables. Python does not accept an assignment as a condition but there are other programming languages that use the same syntax for these two statements, but they do not warn when you use = instead of ==.