惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
博客园 - 【当耐特】
H
Help Net Security
腾讯CDC
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
Last Week in AI
Last Week in AI
Jina AI
Jina AI
博客园 - 聂微东
Blog — PlanetScale
Blog — PlanetScale
A
About on SuperTechFans
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
Y
Y Combinator Blog
C
Check Point Blog
博客园 - 司徒正美
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Python Boolean Expression Evaluation Explained
Abhishek Gan · 2026-05-16 · via DEV Community

TLDR; Boolean Expressions in Python return Python Objects and not True or False values. and expressions return left-most False-valued object or right-most True-valued object. or expressions return left-most True-valued object or right-most False-valued object.

Python has this unique behavior where it returns constituent objects when Boolean operators are used in an expression, instead of a True or False value. In this post, I have illustrated why Python does that. A little context before we dive in...

1. Boolean Expression Syntax

Boolean operators in Python are and and or. A typical Boolean expression looks like:

X <operator> Y

Enter fullscreen mode Exit fullscreen mode

where, the operands X and Y are two Python objects and <operator> is either and or or. A few examples of Boolean expressions are:

True and False
False or True
3 or 39
3 or ['a','b','c']
'dev' and {'first': 'John', 'second': 'Doe'}
'dev' or 0
[] and 'hack'
([] and 'hack') and 'win')

Enter fullscreen mode Exit fullscreen mode


2. Boolean Expression Evaluation

Generally, a Boolean expression containing the and operator evaluates to True only if both the operands are True, and evaluates to False otherwise. Whereas, a Boolean expression containing the or operator evaluates to False only if both the operands are False, and evaluates to True otherwise.
Truth Tables of and and or Boolean Operators are as follows:

Truth Tables of AND and OR Boolean Operators


And now, we dive in...

3. Boolean Expressions In Python

Every object in python has an inherent True or False value. All non-zero and non-empty objects are evaluated as True. For example, 10, 'abc', [1,2,3,4], (1, 'xyz', False), {'name': 'John', 'age': 34}. The number zero i.e. 0 and empty objects such as '', [], {}, () and more are evaluated as False.

In Python, when a Boolean expression is evaluated, it does not return True or False value. Instead, it returns either X or Y i.e. a Python object. The object is returned based on two parameters:

  1. The inherent value of the objects (True/False).
  2. The operator used (and/or).

4. Boolean Expressions Containing and Operator

For expressions containing the and operator, if atleast one object in the expression has an inherent value of False, then the expression returns the first such object that is encountered. The expression will always evaluate to False because False and anything is always False.

Boolean expression with AND operator and atleast one false object

In examples 2 and 3, the final result can be determined by evaluating just the first few objects. The rest of the objects are skipped and the result of the expression is returned. This is called Short-Circuit Evaluation. The evaluation takes a shortcut to the end because the final result could be determined before reaching the end.

If the expression contains only True-valued objects, the last such object is returned by the expression. Python evaluates all the operands from left to right in search of a False object and stops at the last operand. Python evaluates and returns the last object, whether true or false, since it determines the result of the expression.

Boolean expression with AND operator and only true objects


5. Boolean Expressions Containing or Operator

For expressions containing the or operator, if atleast one object in the expression has an inherent value of True, then the expression returns the first such object that is encountered. The expression will always evaluate to True because True or anything is always True.

Boolean expression with OR operator and atleast one True object

Short-Circuit Evaluation can be observed in examples 1 and 2.

If the expression contains only False-valued objects, the last such object is returned by the expression. Python evaluates all the operands from left to right in search of a True object and stops at the last operand. Python evaluates and returns the last object, whether true or false, since it determines the result of the expression.

Boolean expression with OR operator and only false objects


6. Practical Use-Cases

  • Initialize a variable.
a = P or Q or R or None

Enter fullscreen mode Exit fullscreen mode

where, a is the variable being initialized and P, Q, R and None are Python objects. Variable a would be initialized to None only if P, Q, and R are zero or empty objects. None can be replaced with a default, non-empty object.


Thank you for reading!! Comment your thoughts...