October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Check If Two Variables Are True in Python

Use `if a and b:` to require two truthy values in Python. Use identity checks when both variables must be exactly `True`.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use if a and b: when both variables need to be truthy. If you mean both must contain Python’s literal Boolean value True, use if a is True and b is True: instead. The right test depends on what “true” means for your values.

Check whether both variables are truthy

Python lets conditions use values other than True and False. In an if statement, if a and b: checks whether each value evaluates as true:

if a and b:
    print("Both values are truthy")

This is useful when a value should count as present or enabled according to its type’s truth behavior. For example, a nonempty string or a nonzero number is truthy, while an empty string or zero is not. The Python 3.14.7 built-in types reference describes the rules, including how a class can customize truth testing with __bool__() or, when appropriate, __len__().

Examples of truthy and false values

bool(None)      # False
bool(False)     # False
bool(0)         # False
bool("")        # False
bool([])        # False
bool("hello")  # True
bool(3)         # True

Common false values include None, False, numeric zero, and empty built-in sequences and collections. Most other objects are true unless their class defines different truth behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Require both variables to be exactly True

If each variable must be the Boolean singleton True—not merely a value that behaves as true—use identity checks:

if a is True and b is True:
    print("Both are exactly True")

This stricter check rejects values such as 1 and "yes", even though they are truthy. Use it when your requirement specifically calls for Boolean values, rather than when any nonempty or enabled value should pass.

Choose and or or based on the condition

  • a and b requires both values to be truthy.
  • a or b requires at least one value to be truthy.

For example, use if a or b: when either value is enough. Using or when both must pass changes the condition’s meaning.

Python’s Boolean operators short-circuit and return operands, not necessarily Boolean values: x and y returns x if x is false, otherwise it evaluates and returns y. Likewise, x or y returns x if it is true, otherwise it evaluates and returns y. If you need a Boolean result from an expression, use bool(a and b). These behaviors are documented in the Python 3.14.7 expressions reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Write comparisons and compound conditions clearly

Comparisons bind more tightly than not, and, and or. Thus, not a == b means not (a == b). Add parentheses to make a compound condition’s intended grouping clear.

A chained comparison such as a == b == True means a == b and b == True, with the middle expression evaluated once. It does not express the ordinary requirement that both variables be truthy. For that, write a and b; for exact Boolean values, write a is True and b is True.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.