Use the lowercase keyword and as Python’s logical AND operator—equivalent to in C, C++, Java, and JavaScript. It evaluates to truthy only when both operands are truthy.
Basic Example
# 1. Define condition variables
age = 24
has_id = True
# 2. Combine conditions using 'and' in an if statement
if age >= 18 and has_id:
print("Entry allowed") # Output: Entry allowed
Key Details & Mechanics
- Short-circuit evaluation: Python evaluates the left operand first. If it evaluates to a falsy value (
False,0,"",None,[]), Python immediately stops and skips the right operand, preventing unnecessary work or runtime errors:items = [] # Safe: items[0] is never evaluated because len(items) > 0 is False if len(items) > 0 and items[0] == "apple": print("First item is an apple") - Returns an operand, not strictly
bool: Unlikein C-style languages (which always returns a boolean),a and breturnsaifais falsy, andbotherwise. Inside anifstatement, Python automatically checks the truthiness of that returned value. - Operator precedence:
andhas lower precedence than comparison operators (==,!=,<,,in,is) andnot, but higher precedence thanor—so expressions likex > 0 and x < 10work cleanly without extra parentheses.
and vs. (Bitwise AND)
| Operator | Name | Purpose | Example |
|---|---|---|---|
and |
Logical AND | Boolean control flow in if / while statements |
if a > 0 and b > 0: |
|
Bitwise / Element-wise AND | Binary bit manipulation, set intersections, or NumPy/Pandas masks | df[(df["a"] > 0) & (df["b"] > 0)] |
You must log in or register to comment.
