Python Boolean Values

In the last lesson, we learned strings in Python. Now let’s learn how Python represents true and false values.

Life is full of yes and no questions.

Is it raining? Yes or no.

Are we hungry? Yes or no.

Is the door locked? Yes or no.

In Python, these yes/no answers have a special name. They are called boolean values.

And they are simpler than we think. Because there are only two of them.

🔵 The Two Boolean Values in Python

Python has exactly two boolean values:

  • True — means yes
  • False — means no

That’s it. Only two. There is no “maybe.” There is no “sort of.” Just True or False.

is_raining = True
is_sunny = False

Notice the capital letters. True and False must start with a capital letter.

If we write true or false in lowercase, Python will give us an error.

x = true # Error!
x = True # Works

So let’s remember — capital T, capital F. Always.

📖 Why Are They Called Booleans?

They are named after a mathematician called George Boole. He lived in the 1800s. He invented a type of logic based on true and false.

Python borrowed the name. So we call them boolean values. Or just booleans.

But we do not need to remember the history. Just remember — boolean means True or False.

🔍 What Is the Type of a Boolean?

Like integers and strings, booleans have a type.

print(type(True))

Output

<class 'bool'>

bool means boolean. That is the short name.

🧭 Where Do Booleans Come From in Python?

We do not usually write True and False directly. Well, sometimes we do. But most of the time, they come from comparisons.

A comparison is when we compare two things. And the answer is either True or False.

Let me show us.

print(5 > 3)

Output

True

Because 5 is greater than 3. So the answer is True.

print(5 < 3)

Output

False

Because 5 is not less than 3. So the answer is False.

See? Comparisons give us booleans.

⚖️ Comparison Operators in Python

Python has several comparison operators. Let me show us all of them.

Greater than >

print(10 > 5) # True
print(3 > 7) # False

Less than <

print(10 < 5) # False
print(3 < 7) # True

Greater than or equal to >=

print(10 >= 10) # True
print(10 >= 5) # True
print(3 >= 7) # False

Less than or equal to <=

print(10 <= 10) # True
print(10 <= 5) # False
print(3 <= 7) # True

Equal to ==

print(5 == 5) # True
print(5 == 3) # False

Careful here. This is == with two equals signs. Not one.

One equals sign = is for assignment. It means “put this value in this variable.”

Two equals signs == is for comparison. It means “are these two things equal?”

They are different. Let’s not mix them up.

x = 5 # assignment — x is now 5
x == 5 # comparison — is x equal to 5? Yes, True

Not equal to !=

print(5 != 3) # True
print(5 != 5) # False

!= means “not equal.” So 5 != 3 is True because 5 is not equal to 3.

🔤 Comparing Strings in Python

We can compare strings too.

print("apple" == "apple") # True
print("apple" == "orange") # False
print("apple" != "orange") # True

But let’s be careful. String comparison is case sensitive.

print("apple" == "Apple") # False

Because “apple” and “Apple” are not the same. The capital A makes a difference.

If we want to compare without caring about case, we convert both to lowercase first.

print("apple".lower() == "Apple".lower()) # True

🧠 Logical Operators in Python

We can combine booleans using logical operators. There are three: and, or, and not.

and

and gives True only if both things are True.

print(True and True) # True
print(True and False) # False
print(False and True) # False
print(False and False) # False

Think of it like this. “I will go to the party if it is Saturday AND I have money.”

Both must be true. If either one is false, the answer is false.

or

or gives True if at least one thing is True.

print(True or True) # True
print(True or False) # True
print(False or True) # True
print(False or False) # False

Think of it like this. “I will go to the party if it is Saturday OR I have money.”

Just one needs to be true. Only if both are false, the answer is false.

not

not flips the value. True becomes False. False becomes True.

print(not True) # False
print(not False) # True

Simple.

🗳️ Python Booleans: A Real-Life Example

Let’s say we want to check if someone can vote.

The rules are:

  • They must be 18 or older
  • They must be a citizen
age = 20
is_citizen = True
can_vote = age >= 18 and is_citizen
print(can_vote)

Output

True

Because age is 20 (which is >= 18) AND is_citizen is True. Both are true. So the answer is True.

Now if the person was 16:

age = 16
is_citizen = True
can_vote = age >= 18 and is_citizen
print(can_vote)

Output

False

Because age is not >= 18. So the whole thing is False.

That is how and works. Both must be true.

✨ Truthy and Falsy Values in Python

Here is something interesting.

In Python, every value is either “truthy” or “falsy.”

Truthy means it acts like True. Falsy means it acts like False.

Which values are falsy?

  • False
  • None
  • 0 (zero)
  • 0.0 (zero float)
  • "" (empty string)
  • [] (empty list)
  • {} (empty dictionary)
  • () (empty tuple)

Everything else is truthy.

So if we do this:

if "hello":
print("This runs")

It will run. Because “hello” is a non-empty string. It is truthy.

But if we do this:

if "":
print("This runs")

It will not run. Because "" is an empty string. It is falsy.

This is useful. We will see it a lot in real code.

🔄 Converting to Boolean in Python

We can convert any value to a boolean using bool().

print(bool(1)) # True
print(bool(0)) # False
print(bool("hello")) # True
print(bool("")) # False
print(bool(5)) # True
print(bool(0.0)) # False

This follows the truthy/falsy rule. If it is truthy, bool() gives True. If it is falsy, bool() gives False.

🔢 Booleans in Math

Here is a fun fact.

In Python, True is actually equal to 1. And False is equal to 0.

print(True + True) # 2
print(True + False) # 1
print(False + False) # 0
print(True * 5) # 5

Weird, right? But it works.

This is because Python treats booleans as a special type of number. True is 1, False is 0.

We will rarely use this. But it is good to know.

⚠️ Common Mistakes with Python Booleans

💬 Mistake 1: Using Lowercase

x = true # Error!

So what do we do? We use capital letters.

x = True

🖥 Mistake 2: Confusing = and ==

if x = 5: # Error!

So what do we do? We use == for comparison.

if x == 5:

Let’s remember — one equals is for assignment. Two equals is for comparison.

🐍 Mistake 3: Using Quotes Around True or False

x = "True" # This is a string, not a boolean!

So what do we do? We remove the quotes.

x = True

"True" is a string. True is a boolean. They are different.

We can check:

print(type("True")) # <class 'str'>
print(type(True)) # <class 'bool'>

See? Different types.

Next, we will learn about the None Data Type so we can represent a value that is missing or not ready yet.