askvity

How to import Boolean in Python?

Published in Python Boolean Built-in 4 mins read

You don't need to import Boolean in Python. Boolean is a fundamental, built-in data type in the language, readily available for use without any special import statements.

Python includes Boolean values as core components, just like numbers or strings. To create and use Boolean variables or values, you simply use the special Python keywords True and False. These are the only two values that a standard Boolean variable can hold, and they must be capitalized exactly as shown.

Understanding Python's Boolean Type

In Python, the Boolean type (bool) represents truth values. There are only two objects of this type:

  • True
  • False

These aren't just arbitrary names; they are reserved keywords in Python, meaning you cannot use True or False for variable names, function names, or any other identifiers.

Why No Import is Needed

Unlike some modules or specific functions that require an import statement before use (like math or random), the bool type and its values (True and False) are part of Python's standard global namespace. This means they are automatically available as soon as you start writing and running Python code.

Think of True and False like the numbers 1 or 100 – you don't need to import numbers to use them.

Using Boolean Values in Python

You can assign True or False directly to variables, use them in comparisons, or employ them in logical operations.

Assigning Boolean Values

Assigning a Boolean value to a variable is straightforward:

# Assigning True to a variable
is_active = True

# Assigning False to another variable
is_processed = False

print(type(is_active))
print(type(is_processed))

This will output <class 'bool'> for both, confirming their type.

Boolean Values from Comparisons

Comparison operators in Python (like ==, !=, <, >, <=, >=) evaluate to a Boolean value:

x = 10
y = 20

# Comparison resulting in True
is_greater = y > x
print(f"Is y greater than x? {is_greater}")

# Comparison resulting in False
is_equal = x == y
print(f"Is x equal to y? {is_equal}")

Boolean Values in Conditional Logic

Booleans are fundamental for controlling the flow of your program using conditional statements (if, elif, else):

is_logged_in = True

if is_logged_in:
    print("Welcome back!")
else:
    print("Please log in.")

Truthiness and Falsiness

Beyond the explicit True and False keywords, many other Python objects are considered "truthy" or "falsy" in a Boolean context (like in if statements).

Category Falsy Values Truthy Values
Booleans False True
Numbers 0, 0.0, 0j Any non-zero number
Sequences '' (empty string) Non-empty strings
[] (empty list) Non-empty lists
() (empty tuple) Non-empty tuples
{} (empty dict) Non-empty dictionaries
set() (empty set) Non-empty sets
Other Types None Most other objects by default

Understanding truthiness allows you to use objects directly in if statements:

my_list = [1, 2, 3]
if my_list: # my_list is truthy because it's not empty
    print("The list is not empty.")

empty_string = ""
if not empty_string: # empty_string is falsy
    print("The string is empty.")

Summary

In Python, you do not need to import the Boolean type or its values. True and False are built-in keywords available globally. You use them directly to represent logical states, control program flow, and evaluate conditions.

Related Articles