Booleans in Python

basic
Published

May 2, 2024

Python, like many other programming languages, utilizes Boolean values to represent truth and falsehood. These values, True and False, form the bedrock of conditional logic and control flow within your programs. Understanding Booleans is important for writing effective and efficient Python code. This post explores how Booleans work in Python, exploring their uses, comparisons, and common pitfalls.

Understanding Boolean Values

At their core, Booleans are a data type with only two possible values:

  • True: Represents a logical true statement.
  • False: Represents a logical false statement.

These values are case-sensitive; true or FALSE are not valid Boolean literals.

is_valid = True
is_active = False

print(is_valid)  # Output: True
print(is_active) # Output: False

Boolean Operations

Python provides many operators for working with Booleans:

  • and (Logical AND): Returns True only if both operands are True.
a = True
b = False
print(a and b)  # Output: False
print(a and a)  # Output: True
  • or (Logical OR): Returns True if at least one operand is True.
a = True
b = False
print(a or b)  # Output: True
print(b or b)  # Output: False
  • not (Logical NOT): Inverts the Boolean value. not True becomes False, and not False becomes True.
a = True
print(not a)  # Output: False

Boolean Expressions and Conditional Statements

Booleans are essential components of conditional statements, allowing your program to execute different blocks of code based on whether a condition is true or false. The most common conditional statement is the if statement:

age = 20
if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

Here, the expression age >= 18 evaluates to a Boolean value (True if age is 18 or greater, False otherwise). The code within the if block executes only if the condition is True.

Booleans and Comparison Operators

Comparison operators are frequently used to generate Boolean values. These include:

  • == (equal to)
  • != (not equal to)
  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)
x = 10
y = 5

print(x == y)  # Output: False
print(x > y)   # Output: True
print(x != y)  # Output: True

Truthy and Falsy Values

In Python, many values can be implicitly converted to Booleans. Values considered “falsy” evaluate to False in a Boolean context; otherwise, they are considered “truthy” and evaluate to True.

Falsy values include:

  • False
  • None
  • Zero of any numeric type (0, 0.0, 0j)
  • Empty sequences or collections (empty strings, lists, tuples, dictionaries, sets)

All other values are considered truthy.

my_list = []
if my_list: #This checks if the list is not empty (truthy)
    print("List is not empty")
else:
    print("List is empty") #This will execute

Beyond the Basics: Boolean Methods

Some data types in Python have built-in methods that return Boolean values. For example, strings have methods like isalnum(), isalpha(), and isdigit() to check if a string contains only alphanumeric characters, alphabetic characters, or digits, respectively.

my_string = "HelloWorld123"
print(my_string.isalnum()) # Output: True
print(my_string.isalpha()) # Output: False