Skip to main content

Fundamental Data Types: Booleans (True, False)

Following our exploration of Fundamental Data Types: Strings (Part 2), this article introduces another fundamental data type in Python: booleans. We'll cover what booleans are, the concept of "truthiness", and the logical operators used to work with them.


📚 Prerequisites

A basic understanding of Python variables and data types.


🎯 Article Outline: What You'll Master

In this article, you will learn:

  • What booleans are and how to use them.
  • The concept of "truthiness" in Python.
  • How to use boolean operators (and, or, not).
  • How to use the bool() function.

🧠 Section 1: What are Booleans?

A boolean is a data type that can have one of two values: True or False. Booleans are essential for decision-making in your code.

is_active = True
is_admin = False

Booleans are most often the result of comparison operations:

print(10 > 5)  # Output: True
print(10 == 5) # Output: False

💻 Section 2: "Truthiness" in Python

In Python, every value has a "truthiness" to it. This means that any value can be evaluated as either True or False in a boolean context.

"Falsy" values:

  • The number 0
  • An empty string ""
  • An empty list [], tuple (), or dictionary {}
  • The special value None

All other values are considered "truthy".

You can use the bool() function to see the boolean value of any object:

print(bool(0))      # Output: False
print(bool(1)) # Output: True
print(bool("")) # Output: False
print(bool("Hello")) # Output: True

🛠️ Section 3: Boolean Operators

Python provides three logical operators to work with booleans: and, or, and not.

  • and: Returns True if both operands are true.
  • or: Returns True if at least one operand is true.
  • not: Reverses the boolean value.
x = 10
y = 5

print(x > 5 and y < 10) # Output: True
print(x > 10 or y < 10) # Output: True
print(not(x > 5)) # Output: False

🔬 Section 4: The bool() Function

As we saw earlier, the bool() function can be used to convert any value to its boolean equivalent. This is especially useful when you want to check if a list, string, or other object is empty.

my_list = []
if not my_list:
print("The list is empty!")

In this example, not my_list is equivalent to not bool(my_list). Since an empty list is "falsy", bool(my_list) is False, and not False is True.


💡 Conclusion & Key Takeaways

You've now learned about booleans, a fundamental concept for controlling the flow of your Python programs.

Let's summarize the key takeaways:

  • Booleans: Represent True or False.
  • Truthiness: Every value in Python can be evaluated as True or False.
  • Boolean Operators: and, or, and not are used to combine and manipulate boolean values.
  • bool(): The function for converting any value to its boolean equivalent.

Challenge Yourself: Write a script that takes a number as input and prints whether it's positive, negative, or zero.


➡️ Next Steps

In the next article, we'll learn about "Type Conversion (Casting)".

Happy coding!


Glossary (Python Terms)

  • Boolean: A data type that can have one of two values: True or False.
  • Truthiness: The boolean value of an object.
  • Operand: A value that an operator acts on.

Further Reading (Python Resources)