Python Booleans and Comparison Operators

This post introduces Python Booleans and comparison operators for beginners. It covers the True/False data type, the six core comparison operators, truthy and falsy values, and how Booleans drive conditional logic in real programs.

Python Booleans and Comparison Operators

When your code needs to make decisions, it relies on one fundamental question: is something true or false? That is exactly what Booleans handle in Python, and understanding them is a critical stepping stone toward writing programs that actually think and respond to data.

What is a Boolean?

A Boolean is a data type with only two possible values: True or False. Notice the capital letters. In Python, capitalisation matters here. Writing true or false (lowercase) will cause an error because Python treats them as undefined variable names, not Boolean values.

is_connected = True
has_errors = False

print(type(is_connected))  # <class 'bool'>

Booleans are the output of any condition you test in Python. When your program asks "is the user logged in?" or "is this number greater than ten?", Python answers with True or False.

Comparison Operators

Comparison operators let you compare two values and return a Boolean result. These are the building blocks of any conditional logic in your programs.

The Six Core Comparison Operators

  • == (equal to): checks if two values are the same
  • != (not equal to): checks if two values are different
  • > (greater than): checks if the left value is greater than the right value
  • < (less than): checks if the left value is less than the right value
  • >= (greater than or equal to): checks if the left value is greater than or equal to the right value
  • <= (less than or equal to): checks if the left value is less than or equal to the right value

Here is a practical example using each one:

speed = 75
limit = 60

print(speed == limit)   # False
print(speed != limit)   # True
print(speed > limit)    # True
print(speed < limit)    # False
print(speed >= limit)   # True
print(speed <= limit)   # False

A very common beginner mistake is using a single equals sign (=) when you mean to compare. A single = assigns a value to a variable. A double == compares two values. Mixing them up is one of the most frequent bugs in early Python code.

Booleans in Action: if Statements

Comparison operators become truly useful when combined with if statements. Python evaluates the condition and executes code based on whether it is True or False.

interface_status = "up"

if interface_status == "up":
    print("Interface is active.")
else:
    print("Interface is down. Check your connection.")

Even if you have not studied if statements in depth yet, you can see the pattern: the comparison produces a Boolean, and the program responds accordingly.

Truthy and Falsy Values

Python goes a step further. Even non-Boolean values behave like True or False in certain contexts. These are called truthy and falsy values.

Values that Python treats as falsy (behave like False):

  • 0 (integer zero)
  • "" (empty string)
  • [] (empty list)
  • None

Everything else is generally treated as truthy. You can test this with the built-in bool() function:

print(bool(0))        # False
print(bool(42))       # True
print(bool(""))       # False
print(bool("hello"))  # True
print(bool([]))       # False

This matters because Python code often checks conditions without explicitly comparing to True or False. You might see code like if username: rather than if username != "":. Both work, but the shorter form relies on truthy and falsy behaviour.

What's Next

Now that you understand how Python evaluates conditions and returns Boolean results, the natural next step is learning how to combine multiple conditions using logical operators: and, or, and not. These let you build more complex decision-making logic, such as checking whether a device is both reachable and authenticated before proceeding. That is coming up in the next post in this series.