and
Updated March 15, 2026 · Keywords
keyword boolean operator logical
The and keyword is a logical conjunction operator in Python. It evaluates to True only when both operands are truthy; otherwise, it returns the first falsy value it encounters.
Syntax
result = left_operand and right_operand
How It Works
Python’s and doesn’t just return True or False — it returns one of its operands:
- If the left operand is falsy, return it immediately (short-circuit)
- If the left operand is truthy, evaluate and return the right operand
This behavior is called short-circuit evaluation.
# Both are truthy - returns the last value
print(True and True)
# True
print(1 and 2)
# 2
print("hello" and "world")
# world
# First is falsy - returns it, short-circuits
print(False and True)
# False
print(0 and 1)
# 0
print("" and "hello")
# ""
# Second is falsy (first was truthy)
print(1 and 0)
# 0
print("hello" and "")
# ""
Common Use Cases
Conditional execution
name = "Alice"
age = 25
# Only prints if both conditions are true
if name and age >= 18:
print(f"{name} is an adult")
# Alice is an adult
name = ""
if name and age >= 18:
print("This won't print")
# (nothing printed - name is falsy)
Guard clauses
def greet(user):
name = user.get("name")
return name and f"Hello, {name}!"
print(greet({"name": "Alice"}))
# Hello, Alice!
print(greet({}))
# None (empty string is falsy, but returns "" not None)
# Better version with explicit None handling
def greet_safe(user):
name = user.get("name")
return name or "Guest"
print(greet_safe({}))
# Guest
Chained conditions
x = 5
# and has lower precedence than comparison operators
result = x > 0 and x < 10
print(result)
# True
# Parentheses not needed but can clarify
result = (x > 0) and (x < 10)
print(result)
# True
Default values
# Setting a default if the primary value is falsy
name = ""
display_name = name and "Unnamed"
print(display_name)
# ""
# More commonly, use or for defaults
value = None
result = value or "default"
print(result)
# default
Boolean Context
When used in a boolean context (like if), Python treats the result as a boolean using bool():
if "hello" and [1, 2, 3]:
print("Both are truthy")
# Both are truthy
if "" and 100:
print("Won't print")
# (nothing printed)
Short-Circuit Demonstration
The right operand is never evaluated if the left is falsy:
def risky():
print("This function was called!")
return False
result = False and risky()
# Nothing printed - risky() was never called
result = True and risky()
# This function was called!
# False
This is useful for avoiding errors:
numbers = [1, 2, 3]
# Safe - doesn't try to access index 10
result = len(numbers) > 10 and numbers[10]
# False (short-circuits before accessing numbers[10])
# Without short-circuit, this would raise IndexError:
# result = numbers[10] and len(numbers) > 10 # IndexError!
Comparison with Other Languages
In many languages (C, Java, JavaScript), && returns strictly boolean values. Python’s and is more flexible but can surprise developers expecting C-style behavior:
# Python
result = 1 and 2 # Returns 2 (not True)
# JavaScript
# const result = 1 && 2; // Returns true (boolean)
See Also
- bool built-in — convert values to boolean
- all built-in — return True if all elements are truthy
- any built-in — return True if any element is truthy