# and as a functional guarddefsafe_head(lst):
return lst and lst[0]
print(safe_head([1, 2, 3]))
print(safe_head([]))
# Conditional method callclassLogger:
def__init__(self, enabled):
self.enabled = enabled
deflog(self, msg):
self.enabled andprint(f"LOG: {msg}")
logger = Logger(True)
logger.log("active")
logger = Logger(False)
logger.log("suppressed") # prints nothing# De Morgan's laws
a, b = True, Falseprint(f"not (a and b) = {not (a and b)}")
print(f"(not a) or (not b) = {(not a) or (not b)}")
Output
Click "Run" to execute your code
Using 'and' for side effects (like method calls) is a pattern from JavaScript. In Python, explicit if statements are usually preferred for clarity.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?