CSCA08 – Introduction to Computer Programming
Boolean Representation in Python
Atrue/false value is called a Boolean type, named after George BooCheck in Python:
philosopher and logician of the early 19th century.
False == 0
True == 1
In Python we denote the Boolean type by bool. The bool type in PytFalse < True
only two values: True and False.
There are many different operators in Python that evaluate to True or False (note the capital letters).
For each of the following expressions that use conditional operators, write down the result that they
3 < 4
3 > 8
8 > 3
3.5 >= 3.5
7 == 7 # Why not just one equals sign?\\
x = 7
y = 7.0
x == y
3 != 4
False is stored internally as 0, and True as 1.
Even though True is 1, when checking boolean condition, Python
treats any non-zero number as True. if 0:
Examples to try out: if 9.2423: print print
print 'yay' 'yay' 'yay'
num = 6
if num == 10 or 20 or 30:
What does this do? print 'yay'
print 'no way'
CSCA08 – Fall 2010 Booleans and If-Statements in Python Page 1 of 3 Python treats any numeric value not equal to 0 as True. This means that:
False < True if 10.5:
print "10.5 is true!"
prints out 10.5 is true!
If-Statements in Python
In everyday life, we often make decisions based on the current conditions.
If the weather is warm,
I wear shorts.
I wear pants.
The same conditional process appears when writing code. To handle these situations we use an if
Form for writing an if-statement: