Python Booleans

In Python, Booleans represent one of two values: True or False. Boolean values are typically used to represent the result of comparisons or logical operations.

1. Boolean Values

There are only two boolean values in Python:

  • True
  • False

Booleans are useful in control flow structures like if statements and loops.

Example:

x = True
y = False

2. Boolean Data Type

Booleans in Python are of the data type bool.

Example:

x = True
print(type(x))  # Output: <class 'bool'>

3. Boolean Expressions

Boolean expressions evaluate to either True or False. They are often the result of comparison operations.

Example:

x = 5
y = 10

# Comparison operators return boolean values
print(x == y)  # Output: False (because 5 is not equal to 10)
print(x < y)   # Output: True (because 5 is less than 10)

4. Comparison Operators

Comparison operators are used to compare values. They always return True or False.

OperatorDescriptionExample
==Equal tox == y
!=Not equal tox != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

Example:

a = 5
b = 3

print(a > b)   # Output: True (because 5 is greater than 3)
print(a == b)  # Output: False (because 5 is not equal to 3)

5. Logical Operators

Logical operators are used to combine conditional statements. The main logical operators in Python are:

  • and: Returns True if both statements are true.
  • or: Returns True if at least one of the statements is true.
  • not: Reverses the result (returns False if the result is true, and vice versa).

Example:

x = 5
y = 10

# 'and' operator
print(x > 0 and y > 0)  # Output: True (both conditions are true)

# 'or' operator
print(x > 10 or y > 0)  # Output: True (one of the conditions is true)

# 'not' operator
print(not(x > 0))  # Output: False (x > 0 is true, so 'not' makes it False)

6. Truthy and Falsy Values

In Python, certain values are considered truthy (evaluate to True), and others are considered falsy (evaluate to False). The following values are considered falsy:

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

All other values are considered truthy.

Example:

# Falsy examples
print(bool(0))        # Output: False
print(bool(""))       # Output: False
print(bool([]))       # Output: False

# Truthy examples
print(bool(1))        # Output: True
print(bool("hello"))  # Output: True
print(bool([1, 2]))   # Output: True

7. Boolean Functions

Python has several built-in functions that return boolean values:

FunctionDescriptionExample
bool()Converts a value to a boolean (True/False)bool(1)True
isinstance()Checks if an object is of a certain typeisinstance(5, int)True

Example:

# bool() function
print(bool(100))    # Output: True
print(bool(""))     # Output: False

# isinstance() function
print(isinstance(5, int))   # Output: True
print(isinstance("hello", str))  # Output: True

8. Using Booleans in Control Flow

Example of if statement:

x = 10

if x > 5:
    print("x is greater than 5")
else:
    print("x is less than or equal to 5")

Example of while loop:

x = 0
while x < 5:
    print(x)
    x += 1

9. Boolean Operators Precedence

Like in mathematics, operators have precedence (priority). The logical operators’ precedence order is:

  1. not
  2. and
  3. or

Example:

x = 5
y = 10
z = 15

# Logical 'not' is evaluated first, then 'and', then 'or'
print(x > 0 or not(y > 20 and z > 10))  # Output: True

Summary:

  • Booleans are values representing True or False.
  • Booleans are commonly the result of comparison or logical operations.
  • Python considers some values as truthy or falsy.
  • You can use logical operators like and, or, and not to combine multiple conditions.
Leave a Reply 0

Your email address will not be published. Required fields are marked *