Blog Archive

Sunday, October 4, 2020

Python Boolean Operations

Python Boolean Operations: and, or, not 


Boolean operators such and, or, and not are also called logical operators.

" and " and " or "operators requires two variables whereas " not " operators need only one variable. 


1. "and " operator test example :

It is interesting to know that "and" is a short-circuit operator, it checks both the arguments if both the operands are true or both are false then it returns true. Whereas if any one of the operands is true and other operands are false then it returns false.

# boolean "and" operator test program


x= 5
y=10
print(x);
print(y);
if (x>3 and y>1):
   print("true")
else:
   print("false"



2. "or " operator test example :

Like "and" the "or" is also short-circuit operator, it also checks both the argument if any operand is true it returns true. Whereas, if all operands are false then it returns false.


# boolean "or" operator test program

x= 5
y=10
print(x);
print(y);
if (x>10 or y>12):
   print("true")
else:
   print("false"



3. " not " operator test example :

This operator is also called inverter operator which actually clarifies it's nature or function. It just provides the inverted output. Means if an operand is true then it returns false and vice-versa. It requires only one operand.


# boolean "not" operator test program

x= 5
y=10
print(x);
print(y);
if not (x > 10) :
   
print("true")
else:
   
print("flase"