Boolean values are one of the most fundamental concepts in Python programming. They represent one of two possible values: True or False. Although there are only two Boolean values, they play a crucial role in decision-making, comparisons, loops, and program logic.
Whenever Python needs to determine whether a condition is satisfied, it uses Boolean values. For example, checking whether a user is logged in, verifying if a number is greater than another number, or determining whether a file exists all result in either True or False.
Python represents Boolean values using the built-in bool data type. Understanding Booleans is essential because they form the foundation of conditional statements such as if, while, and logical operations, which you will learn in later lessons.
After completing this lesson, you will be able to:
bool) data type.True and False.type() function to check Boolean values.A Boolean is a data type that can store only one of two values:
TrueFalseNotice that the first letter of both values is capitalized. They are predefined keywords in Python, so writing true or false in lowercase will produce an error.
is_student = True
is_logged_in = False
print(is_student)
print(is_logged_in)
Output
True
False
You can verify that a variable is a Boolean by using the type() function.
status = True
print(type(status))
Output
<class 'bool'>
Boolean variables are created in the same way as other variables.
is_admin = True
is_member = False
print(is_admin)
print(is_member)
Output
True
False
Boolean variables are commonly named with prefixes such as is, has, or can, making their purpose clear.
is_active = True
has_license = False
can_vote = True
print(is_active)
print(has_license)
print(can_vote)
Booleans help programs make decisions. Instead of simply storing information, they indicate whether a condition is true or false.
Consider the following situations:
Each of these questions has only two possible answers: True or False.
password_correct = True
print(password_correct)
Output
True
As you continue learning Python, you will use Boolean values extensively with if statements, loops, comparison operators, and logical operators.
In this section, you learned that the Boolean data type (bool) stores only two values: True and False. You created Boolean variables, verified their data type using the type() function, and explored why Booleans are essential for decision-making in programming. In the next section, you will learn how Boolean expressions work, how the bool() function converts values into Boolean values, and what truthy and falsy values mean in Python.
Boolean values are often produced when Python evaluates an expression or compares two values. Instead of assigning True or False manually, Python can determine the result of a comparison automatically.
Python also provides the bool() function, which converts different values into Boolean values. Understanding how Boolean expressions and the bool() function work is essential for writing conditional statements and making decisions in Python programs.
bool() FunctionA Boolean expression is an expression that evaluates to either True or False.
print(10 > 5)
print(10 < 5)
print(20 == 20)
print(15 != 10)
Output
True
False
True
True
Each comparison returns a Boolean value instead of a number or a string.
x = 50
y = 25
print(x > y)
print(x < y)
print(x == y)
Output
True
False
False
bool() FunctionThe bool() function converts a value into either True or False.
print(bool(100))
print(bool("Python"))
print(bool([1, 2, 3]))
Output
True
True
True
Most non-empty values return True.
Some values are considered False by Python.
| Value | Result |
|---|---|
False |
False |
None |
False |
0 |
False |
0.0 |
False |
"" (Empty String) |
False |
[] (Empty List) |
False |
() (Empty Tuple) |
False |
{} (Empty Dictionary) |
False |
set() (Empty Set) |
False |
print(bool(0))
print(bool(""))
print(bool([]))
print(bool(None))
Output
False
False
False
False
Values that are not considered falsy are generally treated as True.
print(bool(1))
print(bool(-25))
print(bool("Hello"))
print(bool([10, 20]))
Output
True
True
True
True
Boolean expressions are commonly used in conditional statements such as if.
age = 18
if age >= 18:
print("Eligible to vote")
Output
Eligible to vote
Here, the expression age >= 18 evaluates to True, so the code inside the if block is executed.
In this section, you learned how Boolean expressions return either True or False when values are compared. You explored the bool() function, understood the difference between truthy and falsy values, and saw how Boolean values are used in conditional statements. In the next section, you will learn how Booleans work with logical operators, discover common mistakes beginners make, and explore best practices for using Boolean values in Python programs.
Boolean values become even more useful when they are combined with logical operators. Logical operators allow you to evaluate multiple conditions together and determine whether an overall expression is True or False.
In Python, logical operators work only with Boolean values. Although they are covered in detail in a separate lesson, it is helpful to understand how they interact with Boolean values.
and OperatorThe and operator returns True only if both conditions are true.
x = 10
y = 20
print(x > 5 and y > 15)
print(x > 15 and y > 15)
Output
True
False
or OperatorThe or operator returns True if at least one condition is true.
x = 10
y = 20
print(x > 15 or y > 15)
print(x > 50 or y > 50)
Output
True
False
not OperatorThe not operator reverses the Boolean value.
is_logged_in = True
print(not is_logged_in)
Output
False
You will study logical operators in detail in a later lesson.
| Expression | Result |
|---|---|
10 > 5 |
True |
5 > 10 |
False |
10 == 10 |
True |
10 != 10 |
False |
8 <= 10 |
True |
true or false instead of True or False.=) instead of the comparison operator (==).True.False."True" or "False".x = 10
print(x = 10)
Incorrect
The correct comparison is:
x = 10
print(x == 10)
Output
True
True and False with capital first letters.is_valid, has_permission, and can_login.True unnecessarily.Instead of writing:
if is_logged_in == True:
print("Welcome")
Write:
if is_logged_in:
print("Welcome")
The second version is shorter, cleaner, and follows Python's recommended coding style.
In this section, you learned how Boolean values work with the logical operators and, or, and not. You explored common Boolean expressions, reviewed frequent beginner mistakes, and learned best practices for writing clean and readable Boolean code. In the final section, you will review the complete lesson through a summary, key takeaways, frequently asked questions, coding exercises, interview questions, a mini project, and a preview of the next lesson on Python Type Conversion.
In this lesson, you learned about the Boolean (bool) data type, which represents one of two possible values: True or False. You explored how Boolean variables are created, how comparison expressions return Boolean values, and how the bool() function converts different values into either True or False.
You also learned about truthy and falsy values, basic logical operators, and the importance of Booleans in decision-making. These concepts form the foundation for conditional statements, loops, and logical operations that you will study in later lessons.
bool data type.True or False.bool() function converts values into Boolean values.0, and None evaluate to False.True.and, or, and not work with Boolean values.A Boolean is a data type that stores either True or False.
The bool data type represents Boolean values.
value = True
print(type(value))
bool() function do?It converts a value into either True or False.
Examples include False, 0, 0.0, None, empty strings, and empty collections.
Yes. Comparison operators always return either True or False.
"True" and True the same?No. "True" is a string, while True is a Boolean value.
They allow programs to make decisions using conditions and logical expressions.
type() function to verify the Boolean data type.bool() function with different values such as 0, 1, an empty string, and a non-empty string.and, or, and not operators with Boolean variables.bool() function?True and "True"?Create a Python program that determines whether a student is eligible for an examination.
Your program should:
and operator.Congratulations! You now understand how Python represents Boolean values, evaluates expressions, converts values using the bool() function, and uses logical operators to combine conditions.
In the next lesson, you will learn Python Type Conversion (Type Casting), where you will explore implicit and explicit type conversion, convert values between different data types, and understand how Python handles mixed data types during calculations.
In this lesson, you learned about the Boolean (bool) data type, which represents one of two possible values: True or False. You explored how Boolean variables are created, how comparison expressions return Boolean values, and how the bool() function converts different values into either True or False.
You also learned about truthy and falsy values, basic logical operators, and the importance of Booleans in decision-making. These concepts form the foundation for conditional statements, loops, and logical operations that you will study in later lessons.
bool data type.True or False.bool() function converts values into Boolean values.0, and None evaluate to False.True.and, or, and not work with Boolean values.A Boolean is a data type that stores either True or False.
The bool data type represents Boolean values.
value = True
print(type(value))
bool() function do?It converts a value into either True or False.
Examples include False, 0, 0.0, None, empty strings, and empty collections.
Yes. Comparison operators always return either True or False.
"True" and True the same?No. "True" is a string, while True is a Boolean value.
They allow programs to make decisions using conditions and logical expressions.
type() function to verify the Boolean data type.bool() function with different values such as 0, 1, an empty string, and a non-empty string.and, or, and not operators with Boolean variables.bool() function?True and "True"?Create a Python program that determines whether a student is eligible for an examination.
Your program should:
and operator.Congratulations! You now understand how Python represents Boolean values, evaluates expressions, converts values using the bool() function, and uses logical operators to combine conditions.
In the next lesson, you will learn Python Type Conversion (Type Casting), where you will explore implicit and explicit type conversion, convert values between different data types, and understand how Python handles mixed data types during calculations.