What are Booleans?
A Boolean is a data type that can hold one of two possible values:
- True: Represents a condition that is correct or satisfied.
- False: Represents a condition that is incorrect or not satisfied.
Python uses Booleans extensively in conditional statements and logical operations.
Example of Boolean Values:
is_python_easy = True
is_java_hard = False
print(is_python_easy) # Output: True
print(is_java_hard) # Output: False
Boolean Values in Python
In Python, many values can evaluate to either True or False.
Values that Evaluate to False:
- None
- False
- Zero of any numeric type: 0, 0.0, etc.
- Empty sequences or collections: ‘ ‘, ( ), [ ], { }
- Objects that are explicitly marked as False using custom logic.
Values that Evaluate to True:
- All other values that are not mentioned above.
Example:
print(bool(None)) # Output: False
print(bool(0)) # Output: False
print(bool("")) # Output: False
print(bool("Python")) # Output: True
print(bool(42)) # Output: True
Using Booleans in Conditional Statements
Booleans play a key role in decision-making using if, elif and else statements.
Example:
is_raining = True
if is_raining:
print("Take an umbrella.") # Output: Take an umbrella.
else:
print("No need for an umbrella.")
Comparison Operators
Comparison operators in Python return Boolean values (True or False) based on the relationship between operands.
Operator | Description | Example |
---|---|---|
== | Equal to | 5 == 5 → True |
!= | Not equal to | 5 != 3 → True |
< | Less than | 3 < 5 → True |
> | Greater than | 5 > 3 → True |
<= | Less than or equal to | 3 <= 3 → True |
>= | Greater than or equal to | 5 >= 3 → True |
Example:
x = 10
y = 5
print(x > y) # Output: True
print(x == y) # Output: False
print(x <= y) # Output: False
Logical Operators
Logical operators are used to combine multiple Boolean expressions.
Operator | Description | Example |
---|---|---|
and | Returns True if both are True | True and False → False |
or | Returns True if one is True | True or False → True |
not | Reverses the Boolean value | not True → False |
Example:
x = 10
y = 20
# Logical AND
print(x > 5 and y > 15) # Output: True
# Logical OR
print(x < 5 or y > 15) # Output: True
# Logical NOT
print(not (x > 15)) # Output: True
Boolean Functions
Python provides built-in functions to work with Booleans.
1. bool() Function:
The bool() function converts a value into its Boolean equivalent.
print(bool(0)) # Output: False
print(bool(1)) # Output: True
print(bool([])) # Output: False
print(bool("Hello"))# Output: True
2. isinstance() Function:
Check if a variable is of a specific type.
x = True
print(isinstance(x, bool)) # Output: True
Practical Examples of Booleans
Example 1: Check If a Number is Even or Odd
number = 6
is_even = number % 2 == 0
if is_even:
print(f"{number} is even.") # Output: 6 is even.
else:
print(f"{number} is odd.")
Example 2: Validate User Input
user_input = ""
if not user_input:
print("Input cannot be empty.") # Output: Input cannot be empty.
Example 3: Combine Conditions
age = 18
has_permission = True
if age >= 18 and has_permission:
print("You are allowed to enter.") # Output: You are allowed to enter.
else:
print("Access denied.")