Check If Two Variables Are True in Python

When I first started working with Python over a decade ago, one of the fundamental tasks I often faced was checking if multiple conditions or variables were true. It’s a common requirement in many real-world Python applications, whether you’re handling user inputs, validating data, or controlling program flow.

In this guide, I’ll share practical, easy-to-follow methods to check if two variables are true in Python. I’ll explain the logic behind each approach and provide full code examples so you can apply them directly in your projects. By the end, you’ll feel confident handling Boolean checks in Python like a pro.

Boolean Variables in Python

Before diving into checking if two variables are true, it’s important to understand what “true” means in Python. Python has a built-in boolean type with two values: True and False. These are often the result of comparisons or logical operations.

Variables can hold boolean values directly:

is_active = True
has_permission = False

Or they can hold values that evaluate to True or False in a boolean context (like numbers, strings, or objects). For example:

x = 5  # Non-zero numbers are True in boolean context
y = 0  # Zero is False in boolean context

When checking if variables are true, you’re often interested in their truthiness rather than strict equality to True.

Method 1: Use the and Operator to Check Two Variables

The easy way to check if two variables are true in Python is by using the logical and operator. It returns True only if both operands are true.

Here’s how I use it:

a = True
b = True

if a and b:
    print("Both variables are True")
else:
    print("At least one variable is False")

Output:

Both variables are True

You can see the output in the screenshot below.

Check If Two Variables Are True Python

Method 2: Explicitly Compare Variables to True

Sometimes, you want to check if variables are exactly True, rather than just truthy. For example, the integer 1 is truthy but not the boolean True.

You can do this by comparing variables directly:

a = True
b = 1  # Truthy but not True

if a is True and b is True:
    print("Both variables are exactly True")
else:
    print("At least one variable is not exactly True")

Output:

At least one variable is not exactly True

You can see the output in the screenshot below.

Check If Two Variables Are True in Python

This method is stricter and useful when you want to avoid truthy values like non-empty strings or non-zero numbers.

Method 3: Use a Function to Check Multiple Variables

When your codebase grows, you might want a reusable function to check if two or more variables are true. Here’s a simple function I use regularly:

def both_true(var1, var2):
    return bool(var1) and bool(var2)

# Example usage
x = "Hello"
y = 10

if both_true(x, y):
    print("Both variables are true")
else:
    print("At least one variable is false")

Output:

Both variables are true

You can see the output in the screenshot below.

Check If Two Python Variables Are True

This function converts variables to a boolean using bool() and then checks both.

Method 4: Handle User Inputs or Real-World Data

In many US-based applications, you often check flags or conditions based on user input or external data. For example, validating if a user is both logged in and has admin rights:

user_logged_in = True
user_is_admin = False

if user_logged_in and user_is_admin:
    print("Access granted to admin panel")
else:
    print("Access denied")

Output:

Access denied

You can see the output in the screenshot below.

Python Check If Two Variables Are True

Here, checking two variables effectively controls access.

Common Pitfalls When Checking Variables in Python

  • Using & instead of and: The & operator is a bitwise operator, not a logical operator. Using & on booleans works, but can cause unexpected behavior with non-boolean types.
  • Comparing with == True unnecessarily: Writing if a == True is redundant. Just write if a.
  • Not considering truthy and falsy values: Remember that values like empty strings “”, zero 0, and empty lists [] evaluate to False in Python.

Practical Example: Check Two Conditions in a Workflow

Let me show a practical example where you check if two conditions are true before proceeding. Suppose you’re building a simple order processing system:

order_paid = True
order_in_stock = True

if order_paid and order_in_stock:
    print("Order can be shipped")
else:
    print("Order cannot be shipped")

Output:

Order can be shipped

If the order is not paid for or not in stock, the system will block shipment.

Summary of How to Check Two Variables Are True in Python

  • Use the and operator for a simple, readable check.
  • Use is True if you need strict boolean equality.
  • Wrap logic in functions for reusability.
  • Always consider the type and truthiness of your variables.
  • Avoid common mistakes like confusing & and and.

By mastering these simple methods, you can write clear, efficient Python code that handles multiple conditions effectively.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.