Chapter 4 - Booleans

The boolean (pronounced BOO-lee-un) data type is arguably the simplest data type. Booleans can have only one of two values: True or False. Booleans are useful for storing any kind of data that is limited to two possible values. For example, a switch is either on (True) or it is off (False). A checkbox is either checked (True) or unchecked (False). A credit card is either current (True) or expired (False).

Boolean Operations

Just like you can use operations on two numbers to get another number (e.g., 2 + 3 = 5), you can use operations on two booleans to get another boolean. The operations for booleans are and, or, and not. The table below shows what these operations look like in Python and in other programming languages.

Common Boolean Operations
Operation (Python)Operation (Other)Result
x and yx && yAnd (if both are True, it will return True)
True and True = True
True and False = False
False and True = False
False and False = False
x or yx || yOr (if at least one is True, it will return True)
True or True = True
True or False = True
False or True = True
False or False = False
not x!xNot (if x is False it will return True)
not False = True
not True = False

Why would you use these operations? Pretend you're making a website where only people ages 12+ can join, and they have to check a box saying that they have read the terms and conditions. When they click the button to join, you want to check if they meet both conditions:

is_old_enough = True
is_box_checked = True
is_valid_user = is_old_enough and is_box_checked

In the case above, is_valid_user will evaluate to True because both operands are True. If either is_old_enough is False or is_box_checked is False, then is_valid_user is also False. These operations are very useful to understand when programming conditionals and loops, which will be discussed in later chapters.

Use the coding exercise below to change the values of x and y to better understand how boolean operations work.

# Change the values of x and y x = True y = True # This code performs the operations x_and_y = x and y x_or_y = x or y not_x = not x # This prints the results print("x = " + str(x) + ", y = " + str(y)) print("x and y = " + str(x_and_y)) print("x or y = " + str(x_or_y)) print("not x = " + str(not_x)) print("")

Number Comparisons

The last chapter discussed number operations that produce numbers. Well, there are also number operations that produce booleans. The answer to the question "is x less than y?" for two numbers x and y is going to be a boolean: True or False.

Common Number Comparisions
x > yTrue if x is greater than y
x >= yTrue if x is greater than or equal to y
x < yTrue if x is less than y
x <= yTrue if x is less than or equal to y
x == yTrue if x is equal to y
x != yTrue if x is not equal to y

Change the values in the coding exercise below to make every number comparison evaluate to False.

# Change these values # Make the exercise print False 6 times a = 4 b = 6 c = 3 d = 4 e = 7 f = 3 # This code performs the operations (don't change) var1 = a > b var2 = a >= d var3 = c < e var4 = c <= f var5 = a == d var6 = e != c # This prints the results (don't change) print(var1) print(var2) print(var3) print(var4) print(var5) print(var6) print("") test_output_contains("False\nFalse\nFalse\nFalse\nFalse\nFalse", False, "Output should show False 6 times") success_msg("Great job!")
  • Make a less than or equal to b
  • Make a less than d
  • Make e equal to c
  • Make c greater than f