Booleans in Python
? Booleans in Python
In Python, Booleans (bool type) represent True or False values, which are used in logical operations and decision-making.
1?? Boolean Values
x = Truey = Falseprint(type(x)) # Output: <class 'bool'>2?? Boolean in Comparisons
print(10 > 5) # Trueprint(10 == 5) # Falseprint(10 < 5) # False3?? Boolean with Logical Operators
a = Trueb = Falseprint(a and b) # False (Both must be True)print(a or b) # True (At least one must be True)print(not a) # False (Negates the value)4?? Boolean as Numbers
Trueis equivalent to1Falseis equivalent to0
print(True + 5) # Output: 6 (1 + 5)print(False * 10) # Output: 0 (0 * 10)5?? Truthy & Falsy Values
In Python, some values are treated as False:
0(integer)0.0(float)""(empty string)[](empty list){}(empty dictionary)None
Everything else is True.
print(bool(0)) # Falseprint(bool("")) # Falseprint(bool("Hi")) # Trueprint(bool([1, 2])) # True? Use Case: Conditional Statements
age = 18if age >= 18: print("You are an adult.") # This will executeelse: print("You are a minor.")? Booleans are essential in control flow, conditions, and logic in Python. Let me know if you need more details!