Booleans

This lesson introduces you to the built-in data type bool. Booleans are essential in computer science and take on either a value of True or False.

True and False are keywords in Python and are used to represent their respective boolean values. You can assign them to variables, for example:

my_bool = True
type(my_bool)

The above code assigns the value True to my_bool and then checks the type. Try this in the terminal and you will see that my_bool is now an object of type <class 'bool'>.

1. Boolean Expressions

You can compare two objects of the same type using the comparison operators listed in the table below. These expressions will return either True or False and are known as boolean expressions.

1.1 Comparison Operators

OperatorNameExample
==Equalx == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

1.2 Comparing Numbers

For example, the following expressions compare int objects and evaluate to:

ExpressionResult
3 < 5True
3 > 3False
6 >= 6True
3 == 5False
3 != 5True

1.3 Comparing Strings

Interestingly you can also compare other objects such as str. The following,

"held" < "helm"

evaluates to True and,

"help" < "helm"

evaluates to False.

Python knows how to compare the order of strings! It looks at each character in turn and compares their order in the alphabet. Try experimenting. For example, what do the following two expressions return?

"hel" < "helm"
"hello" < "helm"

Try to reason about the answers that you get.

1.4 Comparing Booleans

Believe it or not, you can also compare the order of Booleans.

True < False      # (Evaluates to False)
False < True      # (Evaluates to True)

This is because Python also represents True as the bit value 1 and False as the bit value 0. Now the above should make sense!

1.5 Comparing Different Types

Do you know how to order the str "hello" and the int 10? I don't and neither does Python.

"hello" < 10     

results in the following exception:

TypeError: '<' not supported between instances of 'str' and 'int'

Two objects you can mix are numbers (int and float),

4 < 5.6       # (Evaluates to True)

and numbers and booleans (bool),

4 < True      # (Evaluates to False)

because True is also represented by 1.

2. Logical Operators

Boolean expressions can be combined with logical operators to create larger boolean expressions:

OperatorDescriptionExample
andReturns True if both statements are Truex < 5 and x < 10
orReturns True if one of the statements is Truex < 5 or x < 10
notReverse the result, returns False if the result is Truenot(x < 5 and x < 10)

2.1 Order of Precedence and Left-to-Right

All the logical operators given above have lower precedence than the arithmetic operators and comparison operators.

You will also notice that the order of precedence (high to low) for the logical operators is not, and and then or. This means you evaluate not first, then and, then or.

You also evaluate left-to-right.

OperatorName
()Parentheses
**Exponentiation
*, /, %, //Multiplication, Division, Modulus, Floor Division
+, -Addition, Subtraction
==, !=, <, >, <=, >=Comparison Operators
notLogical NOT
andLogical AND
orLogical OR

2.1 Evaluating a Complicated Boolean Expression

The table gives quite a complicated expression for the not example.

not(x < 5 and x < 10) 

Let's look at this for x = 4,

# This is not code, we are manually evaluating to see how Python works with this expression

not(x < 5 and x < 10)     # (Substitute x = 4)
not(4 < 5 and 4 < 10)     # (Evaluate 4 < 5)
not(True and 4 < 10)      # (Evaluate 4 < 10)
not(True and True)        # (Evaluate True and True)
not(True)                 # (Evaluate not True)
False 

and for x = 6,

# This is not code, we are manually evaluating to see how Python works with this expression

not(x < 5 and x < 10)     # (Substitute x = 4)
not(6 < 5 and 6 < 10)     # (Evaluate 6 < 5)
not(False and 6 < 10)      # (Evaluate False and ....)
not(False)                 # (Evaluate not False)
True 

False and 6 < 10 evaluated to False because and requires both expressions to be True. As the first is False, why bother evaluating the second?

This is known as short-circuit evaluation or McCarthy evaluation. Named after the great John McCarthy.

2.2 Truth Tables

We can consider the result of combining two bool variables p and q. The following are known as truth tables and display the result for different combinations of p and q using and and or.

2.2.1 Truth Table for and

pqp and q
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

2.2.2 Truth Table for or

pqp or q
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

=== TASK ===

For this program you should fix the single line instructed.

Copy the following code into a new Python file.

# DO NOT TOUCH THESE LINES. THEY ARE USED FOR THE INPUT
is_raining = bool(int(input()))
no_hat = bool(int(input()))
#######################################################

# You should fix this line to by forming an expression using is_raining and no_hat to produce the correct result for takes_umbrella. 

# e.g takes_umbrella = is_raining or no_hat

# Note you only have to fix this line. No if statements etc.. required!

takes_umbrella = True

print(takes_umbrella)

Sam doesn't like getting his hair wet and sometimes wears a hat.

  • On days that it is raining and Sam is not wearing a hat, Sam takes his umbrella.
  • On days that it is raining and Sam is wearing a hat, Sam does not take an umbrella.
  • If it is not raining Sam does not take an umbrella.  

We use two variables is_raining and no_hat to represent whether it is raining and if Sam is wearing a hat.

  • If it is raining is_raining = True
  • If Sam is NOT wearing a hat no_hat = True.  

Using a third variable takes_umbrella determine if Sam should take his umbrella by combining is_raining and no_hat.

For example, on days that it is raining and Sam is not wearing a hat the variables will have the following values:

  • is_raining = True
  • no_hat = True
  • takes_umbrella = True  

is_raining and no_hat have been set up for you. Combine them with logical operators to get the correct value of takes_umbrella.

HINT: You should consider the truth table and fill in the missing entries. This will then give you a hint to what the expression should be.

is_rainingno_hattakes_umbrella
FalseFalse?
FalseTrue?
TrueFalse?
TrueTrueTrue

W3Schools - Python Booleans

W3Schools - Python Comparison Operators