Python Booleans

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.

OperatorDescriptionExample
==Equal to5 == 5 → True
!=Not equal to5 != 3 → True
<Less than3 < 5 → True
>Greater than5 > 3 → True
<=Less than or equal to3 <= 3 → True
>=Greater than or equal to5 >= 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.

OperatorDescriptionExample
andReturns True if both are TrueTrue and False → False
orReturns True if one is TrueTrue or False → True
notReverses the Boolean valuenot 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.")

Leave a Comment