Python Booleans: True and False Values
What is a Boolean in Python?
A Boolean is a data type that represents one of two possible values: True or False.
Booleans help your code make decisions. You can use them to create programs that do different things based on specific conditions. For example, you can check if a user is logged in before you show them a dashboard.
How Python Creates Boolean Values
You usually get a Boolean value through a comparison. Comparison operators look at two values and return True if the statement is accurate, or False if it isn't. You don't normally type True or False directly - instead, Python evaluates an expression and gives you one of them.
Example: Simple Comparisons
See how Python evaluates these expressions by running the code below.
print(10 > 9)
print(10 == 9)
print(10 < 9)
Your output will appear here...
Want to master Python fundamentals? This lesson is part of our Free Python course covering variables, data types, booleans functions, and more.
Explore this free Python course →Primary Comparison Operators
Here is how to use the primary comparison operators in Python to get Booleans:
==(Equal to): Checks if two values are equal. For example,10 == 10returnsTrue.!=(Not equal to): Checks if two values are not equal. For example,10 != 5returnsTrue.>(Greater than): Checks if the value on the left is greater than the value on the right. For example,10 > 5returnsTrue.<(Less than): Checks if the value on the left is less than the value on the right. For example,5 < 10returnsTrue.>=(Greater than or equal to): Checks if the left value is greater than or equal to the right value. For example,10 >= 10returnsTrue.<=(Less than or equal to): Checks if the left value is less than or equal to the right value. For example,5 <= 10returnsTrue.
Example:
user_age = 25
is_adult = user_age >= 18
print(is_adult)
account_balance = 50
can_withdraw = account_balance > 100
print(can_withdraw)
Your output will appear here...
Understanding 'Truthy' and 'Falsy' Values
In Python, some values act like False even though they're not the Boolean value False. We call these "falsy" values. Everything else acts like True, and we call them "truthy."
Falsy Values
Python considers these values as False:
- The special value
None - The Boolean value
False - The number zero:
0and0.0 - Empty text:
"" - Empty lists:
[] - Empty dictionaries:
{}
Truthy Values
Python considers any value that's not on the "falsy" list as True. Here are some examples:
- Non-empty text like
"hello" - Any number except zero, like
1,-10, or0.5 - Lists with items like
[1, 2] - The Boolean value
True
Example: Testing Truthy and Falsy
You can test if a value is truthy or falsy using the bool() function.
print(bool(0)) # False
print(bool(15)) # True
print(bool("")) # False
print(bool("text")) # True
print(bool([])) # False
print(bool([1])) # True
Your output will appear here...
- Notice that Zero (
0), Empty text (""), and Empty brackets ([]) all resulted inFalse. - Any variable containing real data (like
15or"text") resulted inTrue.
Python treats empty values as False because they represent “no data.”
Non-empty values are considered True because they indicate useful or meaningful information.
This behavior makes it easy to check whether a variable contains data without writing extra conditions.
You're making great progress! Ready to tackle more Python challenges with real-world Exercise?
Python ExerciseHow Functions Return Booleans
You can create functions that return a Boolean value. This approach helps you write clean, reusable code that checks for specific conditions.
def is_of_legal_age(age):
return age >= 21
can_buy_alcohol = is_of_legal_age(25)
if can_buy_alcohol:
print("Purchase approved.")
else:
print("Purchase denied.")
Your output will appear here...
Using Booleans with Logical Operators
Logical operators (and, or, not) let you combine multiple Boolean expressions. You use them when you need to check more than one condition.
1. and Operator
The and operator returns True only when both conditions are True. If either condition is False, the entire expression becomes False.
2. or Operator
The or operator returns True when at least one of the conditions is True. It returns False only when both conditions are False.
3. not Operator
The not operator flips a Boolean value. It turns True into False and False into True.
Example: Logical Operators in Action
# AND
print(22 >= 18 and True)
# OR
print(True or False)
# NOT
print(not False)
Your output will appear here...
Common Mistakes to Avoid
You can write cleaner and more reliable code when you avoid these common mistakes with Booleans:
- Confusing assignment (
=) with comparison (==): The single equals sign assigns a value. The double equals sign checks for equality. When you use=instead of==in a comparison, you create bugs. - Forgetting to capitalize
TrueandFalse: Python's Boolean keywords are case-sensitive. When you usetrueorfalse, Python raises aNameError.
Final Challenge: Discount Eligibility
Apply your logic skills! Write a check to see if a customer gets a discount.
Your Task:
- A customer is eligible for a discount if they are a student OR if they are a senior (over 60).
- Use logical operators to calculate the Boolean result.
is_student = False
age = 65
has_discount = is_student or age > 60
print(has_discount)
Your output will appear here...
Lesson Completed
You have successfully learned about Python Booleans, and how to control program logic using True and False values.
Full Python Course
Master Python with 11+ hours of content, 50+ exercises, and real-world projects.
Enroll Now