Python - Operator Boolean operators

Introduction

Operator Description
X and YIs true if both X and Y are true
X or YIs true if either X or Y is true
not X Is true if X is false (the expression returns True or False)

Here, X and Y may be any truth value, or any expression that returns a truth value.

Boolean operators are typed out as words in Python.

It is not like C's &&, ||, and !.

Boolean and/or operators return a true or false object in Python, not the values True or False.

Demo

print( 2 < 3, 3 < 2)# Less than: return True or False (1 or 0) 
print( 2 and 3, 3 and 2 ) # Return left operand if false 
                          # Else, return right operand (true or false) 
print( [] and {} )
print( 3 and [] )

Result

Related Topics