Sobes.tech
Junior

What do the logical operators and, or, and not return?

sobes.tech AI

Answer from AI

The and operator returns the left value if it is falsy, otherwise the right. The or operator returns the left value if it is truthy, otherwise the right. The not operator always returns a boolean value (True or False).

# Examples for and
print(0 and 5)      # 0 (left falsy)
print(1 and 5)      # 5 (left truthy, returns right)
print([] and [1])   # [] (left falsy)

# Examples for or
print(0 or 5)       # 5 (left falsy, returns right)
print(1 or 5)       # 1 (left truthy)
print([] or [1])   # [1] (left falsy, returns right)

# Examples for not
print(not 0)        # True
print(not 1)        # False
print(not [])       # True
print(not [1])      # False