In this article, we will discuss Python Booleans
Boolean represents one of two values: True or False. In Python when you evaluate an expression using comparison operator, logical operators, Identity operators or Membership operators, the value returned is either True or False.
Printing Boolean Value using comparison operators
Code:
a = 3 b = 4 print(a == b) print(a != b) print(a > b) print(a < b) print(a >= b) print(a <= b)
Output:
False True False True False True
Printing Boolean Value using logical operators
Code:
a = 3 b = 4 print(a < b and a < 10) print(a > b or a < 5) print(not(a > b or a < 5))
Output:
True True False
Printing Boolean Value using Identity operators
Code:
a = 3 b = 4 print(a is b) print(a is not b)
Output:
False True
Printing Boolean Value using Membership operators
Code:
a = [2, 3, 4, 5] b = 4 c = 1 print(b in a) print(c not in a)
Output:
True True