What is a Boolean?

In programming, a boolean is a variable type that is either true or false. One can think of a boolean as a button: When it is on, it returns true, and when it is off, it returns false, as shown below:

Text

Booleans are compatible with something called relational operators. This means that booleans can be compared to each other with said relational operators, shown in the code below:

if 1 == 0:
    print("True")
else:
    print("False")
    
# The operator "Not equal to" checks if two booleans do not have the same value.
if 1 != 0:
    print("True")
else:
    print("False")
    
# The operator "Greater than" checks if a boolean is greater than another.
if 1 > 0:
    print("True")
else:
    print("False")
    
# The operator "Less than" checks if a boolean is less than another.
if 1 < 0:
    print("True")
else:
    print("False")
    
# The operator "Greater than or equal to" checks if a boolean is greater than or equal to another
if 1 >= 0:
    print("True")
else:
    print("False")
    
# The operator "Less than" checks if a boolean is less than or equal to another.
if 1 <= 0:
    print("True")
else:
    print("False")
False
True
True
False
True
False

Any condition in an "if" statement is technically a boolean. In the code above, "1 <= 0" was a boolean that has the value of False, since the number 1 is neither less than or equal to the number 0. However, it should be mentioned that in programming, boolean works for all variable types, such as a string, as shown in the code below:

first_message = "Hello World"
second_message = "Hello, World!"

if first_message == second_message:
    print("The two messages are the same")
else:
    print("The two messages are not the same")
The two messages are not the same

Booleans can be used to check if something returns true or false, whether it be simple programs as the ones above or complex programs found in miscellaneous applications.

What are Conditionals?