Lesson 03 - Booleans

The following topics are discussed in this notebook:

  • An introduction to the boolean data type.
  • Comparisons
  • Comparing different data types.
  • Arithmetic with boolean values.

Additional Resources

Introduction to boolean values.

In [1]:
T = True
print(type(T))
<class 'bool'>
In [2]:
F = False
print(type(F))
<class 'bool'>

Comparisons

In [3]:
# Less than
2 < 6
Out[3]:
True
In [4]:
# Greater than
2 > 9
Out[4]:
False
In [5]:
# Equality operator
1 == 7
Out[5]:
False
In [6]:
6 == 2*3
Out[6]:
True
In [7]:
# Not equal
6 != 10 - 3
Out[7]:
True

Comparing ints and floats

In [8]:
a = 17
b = 17.0
In [9]:
print(type(a))
print(type(b))
<class 'int'>
<class 'float'>
In [10]:
a == b
Out[10]:
True

Comparings strings and ints

In [11]:
str7 = "7"
int7 = 7
In [12]:
print(type(str7))
print(type(int7))
<class 'str'>
<class 'int'>
In [13]:
str7 == int7
Out[13]:
False

Comparing two strings

In [14]:
"Blah" == "blah"
Out[14]:
False
In [15]:
"blah" == "blah"
Out[15]:
True

Arithmetic with boolean values

When used in a numerical calculation, boolean values are interpreted as numbers in the following way:

  • 'True' evaluates to 1
  • 'False' evaluates to 0
In [16]:
False + True
Out[16]:
1
In [17]:
False * True
Out[17]:
0
In [18]:
True * 2
Out[18]:
2
In [19]:
False * 5
Out[19]:
0