A boolean (bool) data type is an essential programming concept. The binary data type represents two values: true or false. The two states help control programming flow, conditional statements, and the decision-making process in a program.
This article provides a comprehensive introduction to the boolean data type.
What Is a Boolean Data Type?
A boolean is a data type with two possible values: true (1) or false (0). The two values help represent truth conditions found in logic control structures. The name comes from a branch of mathematics called Boolean algebra, named after George Bool.
The purpose of boolean values is to represent binary test conditions and decisions in a program. The two conditions are essential concepts in programming, algorithms, control structures, data structures, and logical operations.
Booleans vs. Text vs. Numbers
The main difference between booleans, text, and numbers is:
- The boolean data type has two values to represent a logical state.
- A text is any character or string-type data.
- Numbers are numerical values.
All three datatypes are implemented differently and have different available operations.
However, every programming language implements the context of true and false values. Text and number values can have a boolean context; such expressions are considered truthy or falsy. Specific rules for evaluating an expression's value in a boolean context vary between programming languages.
For example, in Python, the boolean data type is
False for false values or
True for true values. Comparing the value pairs results in a
True statement, indicating they have the same logical value:
print(0 == False) print(1 == True)
Any other number or text value compared to
True results in a
False logical value:
print(5 == 1) print(5 == 0) print(5 == True) print(5 == False)
In a boolean context, such as an
if statement condition, any number except
0 evaluates to a
True logical value. For example:
if 5: print("True") else: print("False")
Similar behavior happens with textual data (strings). An empty string evaluates to
False, while a text with any number of characters is
For most programming languages, zeros, none, or empty values are falsy, while everything else is a truthy value.
Booleans are a binary data type with a total of two values. Using booleans, a programmer represents two opposite programming states or logical values, such as:
Boolean value implementations and syntax are different depending on the programming language.
The boolean data type uses specific logical operations that come from Boolean algebra. The operators evaluate the truth value of one or more statements.
Below is a brief overview of the different boolean operators and their behavior.
The AND operator is a binary boolean operator that requires both statements to evaluate to true for the operation to pass. In all other cases, the procedure fails.
The truth table below shows the result of an AND operation between all possible statement combinations.
|x||y||x AND y|
Alternatively, check how the operator works in Python with the following:
print(True and True) print(True and False) print(False and True) print(False and False)
The code prints the result
True for the first statement and
False for the rest.
The OR operator is a binary boolean operator that requires either argument to evaluate to true for the operation to pass. If both values are false, the statement fails.
Below is the truth table and result of an OR operation for all statement combinations.
|x||y||x OR y|
To show the logical value of the statements in Python, use the following code:
print(True or True) print(True or False) print(False or True) print(False or False)
All operations in the code return
True except for the last one.
The NOT operator is a unary boolean operator that inverts the original truth value of a statement. The truth table for the NOT operator is below:
To demonstrate the behavior in Python, use the following code:
print(not True) print(not False)
The code prints the inverse of the logical value.
Boolean in Programming
The boolean data type helps control the decision-making process in programming. Combine boolean logical values with conditional statements and comparison operators to check for complex conditions.
Below are several programming examples that use boolean values to make a decision.
Example 1: AND Operator and if Statement
Use the AND operator for any statement that requires multiple true conditions to pass. For example, the following code asks a user to input a number and checks for two conditions:
number = int(input("Enter a number: ")) if number > 5 and number < 10: print("It is between 5 and 10.") else: print("It is not between 5 and 10.")
The AND operator in the
if statement compares the number for two conditions:
- Is the number greater than five (
number > 5)?
- Is the number also less than ten (
number < 10)?
If both conditions are met, the statement passes.
Example 2: OR Operator and if Statement
Use the OR operator when there are multiple conditions; at least one should be true for the program to continue. For example, the following code requires either condition to be true:
number = int(input("Enter a number: ")) if number > 5 or number == 0: print("It's greater than five or exactly zero.") else: print("It's less than five and not zero.")
The program takes user input and checks the number for two conditions:
- Is the number greater than five (
number > 5)?
- If not, is the number precisely zero (
number == 0)?
If either of the conditions passes, the check is successful.
Example 3: NOT Operator With Membership Operator
Use the NOT and membership operators (
in) to check whether an element is not in an iterable object. For example, the following code checks whether a sentence does not contain the letter "a":
sentence = input("Enter a sentence: ") if "a" not in sentence: print('There is no letter "a".') else: print('There is at least one letter "a".')
Use the same method to check for values in any iterable, such as lists or arrays.
After reading this article, you know what the boolean data type is and how it is used in programming. Different programming languages implement the boolean data type in various ways.
For further reading, check out MySQL data types and Python data types to compare the implementations.