We can use the if
statement in the following ways:
>>> if 42 < 0: ... print('Negative') ... else: ... print('Not negative') ... Not negative
In the preceding example, Python decided that 42
is not negative. The else
clause is optional. The comparison operators are equivalent to the ones in C++, Java, and similar languages.
>>> a = -42 >>> if a < 0: ... print('Negative') ... elif a == 0: ... print('Zero') ... else: ... print('Positive') ... Negative
This time, Python decided that 42
is negative.