The statement if expression: begins a control block that executes only if expression is true. You can enclose the expression in parentheses. As far as Python is concerned, the number 0 is false, and any other number is true.
Note that in a condition, we use the == operator to test for equality. The = operator is used only for assignments, and assignments are forbidden within a condition. (Here Python differs from C/C++, which allows assignments inside an if-condition, even though they are usually a horrible mistake.)
In an if statement, an else-clause executes when the condition is not true. For example:
print "MyNumber is even!" else:
print "MyNumber is odd!"
Was this article helpful?