User Guide

138
6.3 Decisions
In Python, making a decision involves calculating whether an
expression is True, or whether it is False. Depending on the
result of that calculation, you can run different blocks of code.
The basic decision-making command in Python is the if
statement.
if variable == value:
doThis()
andThis()
The == operator compares the two values and returns True if
they are the same, or False if they are not. If the result is True
then Python runs the commands below the if statement.
It is very important that you pay attention to the white space that
you use around the commands under an if statement. Use the
Tab key to indent lines that you want to run if the result of the
comparison is True.
You can extend the if statement with another block of code that
runs if the two values are not the same:
if variable == value:
doThisIfTrue()
else:
doThisIfFalse()
There are several other operators that you can use in Python:
Operator Description
!= Returns True if two values are not equal, and False
if they are.
> Returns True if the first number is greater than the
second, and False if it is not.
< Returns True if the first number is less than the
second, and False if it is not.