Holla Tech - Learn

Boolean Logic
 

Boolean logic is used to make more complicated conditions for if statements that rely on more than one condition.
Python’s Boolean operators are and, or, and not.
The and operator takes two arguments, and evaluates as True if, and only if, both of its arguments are True. Otherwise, it evaluates to False

>> 1 == 1 and 2 == 2
True
>>> 1 == 1 and 2 == 3
False
>>> 1 != 1 and 2 == 2
False
>>> 2 < 1 and 3 >  6
False 

 

NOTE!
Python uses words for its Boolean operators, whereas most other languages use symbols such as &&, || and !.

Boolean Or
 

The or operator also takes two arguments. It evaluates to True if either (or both) of its arguments are True, and False if both arguments are False.

>> 1 == 1 or 2 == 2
True
>>> 1 == 1 or 2 == 3
True
>>> 1 != 1 or 2 == 2
True
>>> 2 < 1 or 3 >  6
False 


Boolean Not
 

Unlike other operators we’ve seen so far, not only takes one argument, and inverts it.
The result of not True is False, and not False goes to True.

>> not 1 == 1
False
>>> not 1 > 7
True 

 

NOTE!
You can chain multiple conditional statements in an if statement using the Boolean operators.

BACK NEXT

CLICK ON THE BUTTON BELOW TO GO TO THE PYTHON MAIN COURSE PAGE. 

PYTHON MAIN COURSE PAGE

 


©️ License: All Rights Reserved 


CONTACT HOLLA TECH – LEARN SUPPORT