# and returns the first falsy value, or the last valueprint(1and2and3) # All truthy -> last valueprint(1and0and3) # 0 is falsy -> returns 0print(""and"hello") # ""is falsy -> returns ""print("hi"and"hello") # Both truthy -> "hello"# Practical: guard before access
data = {"users": [{"name": "Alice"}]}
users = data.get("users")
if users andlen(users) > 0and users[0].get("name"):
print(f"First user: {users[0]['name']}")
# and as a conditional expression
debug = True
message = debug and"Debug mode ON"print(message)
Output
Click "Run" to execute your code
'and' short-circuits: it evaluates left to right and returns the first falsy value (or the last value if all are truthy). This makes guard patterns safe.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?