Conditional Statements in Python
Relational Operators
All relational operators are
binary (they operate on two operands). These are:
> (greater than),
>= (greater than or equal to),
< (less than),
<=
(less than or equal to),
== (equal to), and
!= (not equal to).
Operands can be variables or values of any type learned so far. Here we will study the action of
relational operators only on
integer or real variables.
The result of applying a relational operator is always
a logical value -
True (true)
or
False (false).
EXAMPLES
Analyze the result of the following expressions by executing the program:
Note: since Python
is case-sensitive,
be careful with capital letters! The keywords are
True and
False. That's it! Try the command
print(false)...
Boolean Values
As you can see, the truth values
True and
False are of particular importance.
These are called
boolean values, and the concept was first defined by a 19th-century mathematician named
George Boole:
These values are essential in the decisions that a program must make based on user input or values determined within our code.
The result of a logical evaluation is stored in a data type called
bool.
Of course, Python is adaptive, so explicit conversion via
bool(expression) is not necessary.
Still, remember this data type...
Run the program and then proceed to the next page.