Login Register

What Are Booleans In Python?

What are Booleans?

A Boolean (bool) is a special and important data type in Python because it can store only two possible values: True or False

  • True means the statement is correct or the condition is fulfilled.
  • False means the statement is incorrect or the condition is not fulfilled.

You can consider Booleans as a switch: Either ON (True) or OFF (False), which means no middle value.

We can use Booleans everywhere in Python programming, especially in:

Example of Boolean Values:

# Boolean values assigned to variables
course_is_easy = True
internet_is_slow = False

print(course_is_easy) # Output: True
print(internet_is_slow) # Output: False

Explanation of the code:

  • course_is_easy = True → This means you are marking the statement “course is easy” as True.
  • internet_is_slow = False → You are declaring that the statement “internet is slow” is not true.

Boolean Values in Python

If a value contains nothing, zero, or represents emptiness, Python treats it as False, and if a value contains something, Python treats it as True.

Values That Python Treats as False:

  • None = Represents “no value” or “nothing exists here.”
  • False = It is the Boolean false value itself.
  • 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 Python Treats as True

Example of True and False Values:

# Checking the values are False or not:

print(bool(None)) # Output: False (because it's empty)
print(bool(0)) # Output: False (zero means nothing)
print(bool("")) # Output: False (empty string)

# These values contain something, so they are True:
print(bool("Python")) # Output: True (text exists)
print(bool(42)) # Output: True (non-zero number)

How to Use Booleans in Conditional Statements?

Conditional statements (if, elif, else) check these Boolean values and decide what to run further.

Imagine the Booleans gives answers in Yes or No inside your program. For example:

  • “Is the battery low?” → True or False
  • “Did the user enter a number?” → True or False
  • “Is the temperature above 30°C?” → True or False

Example 1:

  • Checking if a shop is open using Booleans
current_hour = 19      # 24-hour format
shop_open = current_hour >= 10 and current_hour <= 20

if shop_open:
print("The shop is open. You can visit now!")
else:
print("The shop is closed. Come back later.")

Explanation:

  • The shop works between 10 AM to 8 PM (20:00)
  • We create a Boolean shop_open based on the time
  • Python checks the Boolean inside if
  • Whichever condition evaluates to True runs

Example 2:

  • Checking if a phone needs charging
battery = 28
needs_charging = battery < 30

if needs_charging:
print("Battery low. Please charge your phone.")
else:
print("Battery level is fine for now.")

Comparison Operators In Booleans

We can also use comparison operators in Python to 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 In Booleans

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 In Python

Python provides important built-in functions to help you test, convert, or verify Boolean values.

1. bool() Function:

The bool() function takes any value and turns it into its Boolean form. In simple words:

  • If the value is “empty”, it converts into → False
  • If the value contains something, it converts into→ True
# Checking if items are present in a cart
cart_items = ["apple", "banana"]
wishlist = []

print(bool(cart_items)) # True (cart has items)
print(bool(wishlist)) # False (empty list)

# Checking a number value
temperature = 0
print(bool(temperature)) # False (0 is treated as False)

score = -5
print(bool(score)) # True (any non-zero number is True)

2. isinstance() Function:

The isinstance() is useful when we want to verify whether a value belongs to a certain data type.

Think of isinstance() as a security guard that can check:

  • “Are you really a Boolean?”
  • “Are you a string?”
  • “Are you a list?”
# Detecting user input type
value = True

if isinstance(value, bool):
print("The value is a Boolean type.")
else:
print("This is not a Boolean.")

# Another example: checking type before math operations
data = "42"

if isinstance(data, int):
print(data + 10)
else:
print("Cannot add numbers because the value is not an integer.")

Practical Examples of Booleans

Example 1: Check Whether a Temperature Is “Safe” or “High”

temperature = 38  # in Celsius
is_high = temperature > 37

if is_high:
print(f"{temperature}°C is high, please rest.")
else:
print(f"{temperature}°C is normal.")

Example 2: Validate a Username Entered by the User

username = "   "  # user entered spaces only

is_valid = bool(username.strip()) # True only if actual text exists

if not is_valid:
print("Username cannot be blank.")
else:
print("Username accepted.")

Example 3: Check If a Person Can Apply for a Job

age = 21
has_degree = True
knows_python = False

eligible = (age >= 18) and has_degree and knows_python

if eligible:
print("You can apply for the job.")
else:
print("You are not eligible.")

Learn Other Topics Also: