The bool Data Type

The bool data type is necessary for the evaluation of logical data (which is inherently different from the numerical or string data types introduced so far). "bool" stands for Boolean data which can only take on one of two possible values: True or False. You should recall from the previous unit that True and False are reserved words within the Python language. Try executing these commands in the Repl.it run window:

a=True
print(a)
print(type(a))
b=False
print(b)
print(type(b))

You should see that True and False appear in your program as blue text indicating that they are reserved Python words. Furthermore, the data type should be identified as bool. Let's investigate how this data type can be used.

Relational operators are those that compare values in order to determine their relationship. Here is a shortlist of very useful relational operators:

  • == Equals 
  • != Not Equals 
  • > Greater Than 
  • < Less Than 
  • <= Less Than or Equal To 
  • >= Greater Than or Equal To

Copy and paste these commands into the Repl.it run window:

a=2
b=3
print(a==b)
print(a!=b)
print(a>=b)
print(a>=a)
print(a<=a)
print(a>b)
print(a < b) 

It should be clear why these are called relational operators. Also, note that the only possible answers when using these operators are either True or False.

Be EXTREMELY careful to NEVER confuse the assignment operator (=) with the relational equals (==) operator. They serve two totally different purposes. Copy and paste and run this set of commands:

a=2
b=5
a=b
print(a)
a=2
b=5
print(a==b)

The assignment operator assigns the numerical value contained in the variable b to the variable a. The relational operator compares the variables a and b for equality which evaluates to the bool data type False.

Finally, there are logical operators that allow us to form combinations of logical expressions. Watch the video for a description of relational and logical operators. Afterwards, you should understand these definitions for logical operators given two boolean variables x and y:

  • not x: Logical complement: if x is True, not x is False. If x is False, not x is True 
  • x and y: Can only be True if both x and y are True, otherwise evaluates to False 
  • x or y: Can only be False if both x and y are False, otherwise evaluates to True

These operators will be incredibly important in the next unit. For now, we just need to practice using them in order to get used to the syntax.

Last modified: Tuesday, November 17, 2020, 4:12 PM