Last updated

Python Booleans

Definition: A boolean is a value that is either True or False. Booleans are the answers to yes/no questions and power every decision your code makes.

Example 1 — comparisons return booleans

print(10 > 9)    # True
print(10 == 9)   # False
print(10 != 9)   # True (not equal)

The comparison operators are == equal, != not equal, >, <, >=, <=.

Example 2 — storing a boolean

age = 20
is_adult = age >= 18
print(is_adult)   # True

Example 3 — truthy and falsy values

Empty values act like False; values with content act like True:

print(bool(0))      # False
print(bool(""))     # False (empty text)
print(bool("hi"))   # True
print(bool(42))     # True

Example 4 — booleans in an if

logged_in = True
if logged_in:
    print("Welcome back!")

💡 Common mistake: one = assigns a value; two == compare. if x = 5 is an error; if x == 5 is a comparison.

Try it Yourself
Output

          
Ad · responsive