Skip to main content

Python Booleans, Comparisons, and All Operators Explained

Beginner25 min6 exercises90 XP
0/6 exercises

Every day you make yes-or-no decisions. "Is it raining?" "Do I have enough money?" "Is my password correct?" Computers make the same kind of decisions, and they use booleans to do it.

A boolean is a value that is either True or False. That's it — just two options. Think of it like a light switch: on or off, yes or no, true or false.

In this tutorial, you'll learn how Python compares values, combines conditions, and decides what counts as "true" or "false." These skills are the foundation for every if statement and loop you'll ever write.

What Are Booleans in Python?

Python has a built-in data type called bool. It has exactly two possible values: True and False. Notice the capital T and capital F — this matters.

Creating boolean variables
Loading editor...

Boolean variables usually start with is_, has_, or can_ to make your code read like plain English. For example, is_logged_in or has_permission tells you exactly what the variable means.


How to Compare Values in Python

Most booleans don't come from typing True or False directly. They come from comparisons. When you compare two values, Python gives you back a boolean.

Think of comparison operators like a judge at a talent show. You give them two things, and they answer "True" or "False."

All six comparison operators
Loading editor...

You can store comparison results in variables. This is useful when you want to check a condition once and use the result multiple times.

Storing comparison results
Loading editor...

How to Compare Strings in Python

You can compare strings too. Python checks them character by character using alphabetical (technically Unicode) order. Uppercase letters come before lowercase letters.

Comparing strings
Loading editor...

How to Use Logical Operators: and, or, not

Sometimes one comparison isn't enough. You might need to check if someone is old enough and has a ticket. Or if it's Saturday or Sunday. Python gives you three logical operators to combine conditions.

The "and" Operator

The and operator returns True only when both sides are true. Think of it like a door with two locks — you need both keys to open it.

Using "and"
Loading editor...

The "or" Operator

The or operator returns True when at least one side is true. Think of it like a door with two doorbells — either one will get someone to open up.

Using "or"
Loading editor...

The "not" Operator

The not operator flips a boolean to its opposite. True becomes False, and False becomes True. It's like flipping a light switch.

Using "not"
Loading editor...

What Is Short-Circuit Evaluation in Python?

Python is lazy in a smart way. When evaluating and or or, it stops as soon as it knows the answer. This is called short-circuit evaluation.

With and, if the first value is False, Python doesn't bother checking the second — the result is already False. With or, if the first value is True, it skips the second — the result is already True.

Short-circuit evaluation in action
Loading editor...

What Are Truthy and Falsy Values in Python?

Here's a surprising fact: in Python, every value has a "truthiness." It's not just True and False that count — every value can be treated as either true or false.

Think of it this way: is your wallet empty or does it have something in it? Empty things are "falsy." Things with content are "truthy."

Falsy vs truthy values
Loading editor...

How to Use the bool() Function

The bool() function converts any value to its boolean equivalent. It tells you whether Python considers a value truthy or falsy.

Using bool() to check values
Loading editor...

In practice, you rarely need to call bool() directly. Python automatically checks truthiness in if statements. Writing if name: is the same as if bool(name): but shorter.

Verbose (unnecessary)
name = "Alice"
if bool(name) == True:
    print("Name is set")
Pythonic (clean)
name = "Alice"
if name:
    print("Name is set")

How to Use Chained Comparisons in Python

Python has a neat trick that most languages don't: you can chain comparisons together, just like you would in math. Instead of writing x > 1 and x < 10, you can write 1 < x < 10.

Chained comparisons
Loading editor...

Chained comparisons read just like math notation. They're shorter and easier to understand at a glance. Use them whenever you're checking if a value falls within a range.


Common Boolean Mistakes in Python

Mistake 1: Using = Instead of ==

This is the most common boolean mistake for beginners. A single = is for assignment (giving a variable a value). A double == is for comparison (checking if two values are equal).

Bug: Assignment instead of comparison
score = 100
# This assigns 50 to score, not a comparison!
# if score = 50:  # SyntaxError!
#     print("You got 50")
Fix: Use == for comparison
score = 100
if score == 50:
    print("You got 50")
else:
    print("Not 50")

Mistake 2: Comparing Different Types

Comparing a string to a number can lead to surprising results. The string "5" is not equal to the number 5. They're different types.

Comparing different types
Loading editor...

Practice Exercises

Check if a Number Is in Range
Write Code

Create a variable num set to 42. Use a chained comparison to check if num is between 1 and 100 (inclusive). Print the result.

Expected output:

True
Loading editor...
Validate a String Length
Write Code

Create a variable word set to "Python". Check if the string is non-empty AND longer than 3 characters. Print the result.

Expected output:

True
Loading editor...
Predict the Logical Output
Predict Output

Without running the code, predict what each line will print. Then run it to check your answers.

The code prints the results of three logical operations.

Loading editor...
Fix the Comparison Bug
Fix the Bug

The code below should check if status equals "active" and print the result. But it has a bug. Find and fix it.

Expected output:

True
Loading editor...
Check for a Leap Year
Write Code

Create a variable year set to 2024. A year is a leap year if:

  • It's divisible by 4
  • But not divisible by 100
  • Unless it's also divisible by 400
  • Use the modulo operator % and logical operators to check. Print the result.

    Expected output:

    True
    Loading editor...
    Evaluate Truthiness with bool()
    Write Code

    Use the bool() function to print the truthiness of these values, each on its own line and in this exact order:

    1. 0

    2. "hello"

    3. ""

    4. 42

    5. None

    Expected output:

    False
    True
    False
    True
    False
    Loading editor...