
Boolean data in Python
Whenever a program decides what to do next, whether to print a message, whether to let a user in, whether to retry a request, it works with boolean values. Python's bool type holds exactly two values: True and False, and every condition and check is built from them.
What is boolean data?
In Python, the values "true" and "false" are represented by the bool type, which can hold only two values:
- True
- False
Python 3.13# Create boolean variables is_raining = True likes_pizza = True print(type(is_raining))<class 'bool'>
Important: True and False are always written with a capital first letter. If you write true or false, Python won't understand them and will throw an error.
Boolean operators
We often need to combine several conditions. For example: "I'll go to the beach if it's sunny AND warm" or "I'll buy this phone if it's beautiful OR cheap". Python gives us three operators for this: and, or, not.
The and operator (logical AND)
Returns True only if both values are true:
Python 3.13sunny = True warm = True # Go to the beach if sunny AND warm going_to_beach = sunny and warm print(f"Sunny: {sunny}, Warm: {warm}")Sunny: True, Warm: Trueprint(f"Going to the beach? {going_to_beach}")Going to the beach? True# What if the weather changes? warm = False # It got cold going_to_beach = sunny and warm print(f"Sunny: {sunny}, Warm: {warm}")Sunny: True, Warm: Falseprint(f"Going to the beach? {going_to_beach}")Going to the beach? False
The or operator (logical OR)
Returns True if at least one value is true:
Python 3.13phone_is_beautiful = True phone_is_cheap = False # Buy the phone if it's beautiful OR cheap will_buy_phone = phone_is_beautiful or phone_is_cheap print(f"Phone is beautiful: {phone_is_beautiful}, Phone is cheap: {phone_is_cheap}")Phone is beautiful: True, Phone is cheap: Falseprint(f"Will we buy the phone? {will_buy_phone}")Will we buy the phone? True
The not operator (logical NOT)
Inverts the value: True becomes False, and vice versa:
Python 3.13have_homework = True print(f"I have homework: {have_homework}")I have homework: Trueprint(f"I do NOT have homework: {not have_homework}")I do NOT have homework: False
Comparing values
Boolean values often come up as the result of a comparison:
Python 3.13# Comparing numbers my_age = 25 friend_age = 30 print(f"My age: {my_age}, friend's age: {friend_age}")My age: 25, friend's age: 30print(f"Are we the same age? {my_age == friend_age}")Are we the same age? Falseprint(f"Are we different ages? {my_age != friend_age}")Are we different ages? Trueprint(f"Am I younger? {my_age < friend_age}")Am I younger? True# Comparing strings (alphabetically) print(f"'apple' < 'banana': {'apple' < 'banana'}")'apple' < 'banana': True
The is operator: comparing identity
Beyond ==, Python also has the is operator. They look similar but check different things:
- == compares values (what's inside)
- is compares identity (whether two names point at the same object in memory)
Python 3.13my_scores = [90, 85, 95] friend_scores = [90, 85, 95] # Same list, but a different object same_list = my_scores # The same object print(f"Same content? {my_scores == friend_scores}")Same content? Trueprint(f"The same object? {my_scores is friend_scores}")The same object? Falseprint(f"Is same_list the same object as my_scores? {my_scores is same_list}")Is same_list the same object as my_scores? True
Rule of thumb: in the vast majority of cases you want ==. The is operator is appropriate only for checks against the special singletons: is None, is True, is False. Using is on numbers, strings, or lists is almost always a bug.
Operator precedence: what's evaluated first?
Operators are applied in this order (from highest to lowest precedence):
- not (highest precedence)
- and
- or (lowest precedence)
Python 3.13# Precedence example has_ticket = True has_passport = False has_visa = True # Can we travel abroad? # We need a ticket AND (passport OR visa) can_travel = has_ticket and (has_passport or has_visa) print(f"has_ticket and (has_passport or has_visa) = {can_travel}")has_ticket and (has_passport or has_visa) = True# Step-by-step evaluation: step1 = has_passport or has_visa # First, the expression in parentheses is evaluated print(f"Step 1: has_passport or has_visa = {step1}")Step 1: has_passport or has_visa = Truestep2 = has_ticket and step1 # Then the and operator is applied print(f"Step 2: has_ticket and (result of step 1) = {step2}")Step 2: has_ticket and (result of step 1) = True
Tip: if you're unsure about the order of evaluation, use parentheses. They make the code clearer and let you control the order explicitly.
Conversion to boolean: what counts as true?
The bool function
Python can convert any value to a boolean:
Python 3.13print(bool(100))Trueprint(bool(0))Falseprint(bool("Hello"))Trueprint(bool(""))False
What is considered true and false?
In Python most values are considered true (True).
The values considered false (False) are only:
- False (the boolean "no")
- None (absence of a value)
- Zero: 0, 0.0, 0j
- Empty containers: "", (), [], {}
Python 3.13money = 0 if money: print("I have money") else: print("My wallet is empty")My wallet is emptyname = "Alex" if name: print(f"Hello, {name}") else: print("Hello, stranger")Hello, Alex
Understanding check
Let's check how well you've absorbed the material:
What will the following expression return?
Python 3.13result = (False or True) and not (False and True or True)
