Python Booleans and Boolean Expressions

Python Booleans

Python booleans are literals (fixed values) like integers, strings etc. What makes the boolean data type interesting is that booleans can only be one of two values: True or False.

Let's first understand what a boolean is and isn't.

# A boolean value, False, is assigned to is_valid 
is_valid = False

# A string, "False", is assigned to is_valid
is_valid = "False"

# The word false is treated as a variable
is_valid = false

Takeaway: True and False should be written without quotation marks and with the first letter capitalized to be a boolean.


Boolean Expressions

The true power of booleans comes from boolean expressions (expressions that evaluate to either True or False). For example,

age >= 18

Read the above code as: Is age greater than or equal to 18?

Questions like "Is the age 18 or above?" or "Is the product in stock?" have only two possible answers: yes or no. In programming, yes is represented as True and no as False, and such questions are expressed using boolean expressions.

In the above code, >= is a comparison operator used to compare two values. Let's look at all the comparison operators next.


Comparison Operators

Here's a list of all the comparison operators that can be used to compare values.

Operator Expression Read as
< x < y Is x less than y?
<= x <= y Is x less than or equal to y?
> x > y Is x greater than y?
>= x >= y Is x greater than or equal to y?
== x == y Is x equal to y?
!= x != y Is x not equal to y?

Let's try out these operators.

Example: < and <=

x = int(input("Enter x: "))
y = int(input("Enter y: "))

result = (x < y)
print(f"x < y ---> {result}")

result = (x <= y)
print(f"x <= y ---> {result}")

result = (x < 10)
print(f"x < 10 ---> {result}")

result = (x <= 10)
print(f"x <= 10 ---> {result}")

Output

Enter x: 10
Enter y: 20
x < y ---> True
x <= y ---> True
x < 10 ---> False
x <= 10 ---> True

Can you figure out why x < 10 gives False but x <= 10 gives True in the output?

Hint:< is "less than" and <= is "less than or equal to".

Example: > and >=

x = int(input("Enter x: "))
y = int(input("Enter y: "))

result = (x > y)
print(f"x > y ---> {result}")

result = (x >= y)
print(f"x >= y ---> {result}")

result = (x > 10)
print(f"x > 10 ---> {result}")

result = (x >= 10)
print(f"x >= 10 ---> {result}")

Output

Enter x: 10
Enter y: 20
x > y ---> False
x >= y ---> False
x > 10 ---> False
x >= 10 ---> True

Can you figure out why x > 10 gives False but x >= 10 gives True in the output?

Hint:> is "greater than" and >= is "greater than or equal to".

Example: == and !=

x = int(input("Enter x: "))
y = int(input("Enter y: "))

result = (x == y)
print(f"x == y ---> {result}")

result = (x != y)
print(f"x != y ---> {result}")

result = (x == 10)
print(f"x == 10 ---> {result}")

result = (x != 10)
print(f"x != 10 ---> {result}")

Output

Enter x: 10
Enter y: 20
x == y ---> False
x != y ---> True
x == 10 ---> True
x != 10 ---> False

The == and != operators can be used to check the equality of not just numbers, but strings as well.

name1 = "Alice"

print(name1 == "Alice")   # True
print(name1 == "alice")   # False
print(name1 != "Alice")   # False
print(name1 != "alice")   # True

Logical Operators

There are three logical operators: and, or & not.

The and Operator

The and operator is used to combine two boolean expressions. The result is True if both expressions are True, and False if either of the expressions is False.

age = int(input("Enter your age: "))
citizen = input("U.S. citizen (yes/no)?: ")

# True only if (age >= 18) 
# and
# (citizen == "yes") is True
result = (age >= 18) and (citizen == "yes")
print(result)

Run the code with different values of age and citizen to see the output for yourself.

The or Operator

The or operator is also used to combine two boolean expressions. The result is True if either of the expressions is True, and False when both expressions are False.

age = int(input("Enter your age: "))
citizen = input("U.S. citizen (yes/no)?: ")

# True if either (age >= 18)
# or
# (citizen == "yes") is True
result = (age >= 18) or (citizen == "yes")
print(result)

Run the code with different values of age and citizen to see the output for yourself.

The not Operator

The not operator reverses a boolean (or boolean expression). If age >= 18 is True, then not (age >= 18) is False.

passcode = "1345"
entered_code = input("Enter passcode: ")

result = (passcode == entered_code)
print(f"Is passcode equal to entered code? {result}")

result = not (passcode == entered_code)
print(f"Is passcode not equal to entered code? {result}")

Output

Enter passcode: 1345
Is passcode equal to entered code? True
Is passcode not equal to entered code? False

The bool() Function

In Python, every value is either truthy or falsy. For example, 0 evaluates to False, while all other numbers evaluate to True if converted to a boolean. You can see this by using the bool() function. The function returns the boolean equivalent of a value.

# 0 is False
print(bool(0))

# Non-zero value is True
print(bool(12))

# Non-empty string is True
print(bool("Python"))
print(bool("False"))  # True

# Empty string is False
print(bool(""))

The "False" string is actually True in a boolean context because it's non-empty. Interesting, isn't it?


Boolean expressions are extensively used in if statements and while loops to build programming logic and control program flow. Be sure to practice this topic thoroughly.

Did you find this article helpful?