Boolean Operators
Boolean Operators
Operators
Boolean operators are operators that return a boolean value (true or false).
Equal To
Python uses the == operator to determine equality. Beginners often confuse
the = and the == operators. Remember, = is the assignment operator.
a = 5
b = 5
print(a == b)
challenge
Not Equal To
The != operator checks to see if two values are not equal.
a = 5
b = 5
print(a != b)
challenge
Less Than
The < operator is used to check if one value is less than another value.
a = 5
b = 7
print(a < b)
challenge
a = 5
b = 7
print(a <= b)
challenge
Greater Than
The > operator is used to check if one value is greater than another value.
a = 9
b = 17
print(a > b)
challenge
a = 9
b = 17
print(a >= b)
challenge
a = True
b = True
c = False
print(a and b)
challenge
a = True
b = True
c = False
print(a and b and c)
challenge
The or Operator
The or operator allows for compound (more than one) boolean expressions.
If only one boolean expressions is true, then the whole thing is true. To be
false, all boolean expressions must be false.
a = True
b = True
c = False
d = False
print(a or b)
challenge
Multiple or Statements
You can chain several or statements together. They are evaluated in a left-
to-right manner.
a = True
b = True
c = False
print(a or b or c)
challenge
print(not True)
challenge
Short Circuiting
If Python can determine the result of a boolean expression before
evaluating the entire thing, it will stop and return the value.
Short Circuiting
Formative Assessment 1
Formative Assessment 2