Lessons

Python Basics

Python Variables

Operators in Python

Conditional Statements in Python

Python Lists

Python Tuples

Python Sets

Python Dictionaries

Loops in Python

Python Arrays and Functions

Conclusion

Python Boolean Type

Python Booleans

Booleans in Python represent one of two values: True or False. Booleans are useful in decision-making, conditions, and logic control, allowing programs to make comparisons and evaluate expressions.

Python Boolean Values

In Python, you often need to determine if an expression is True or False. Any comparison or logical statement results in a Boolean value.

Example: Boolean Comparisons

When comparing two values, Python evaluates the expression and returns either True or False:

python
1
2
3
print(10 > 9)  # True
print(10 == 9)  # False
print(10 < 9)  # False

Evaluating Values and Variables

The bool() function can evaluate any value or variable to determine if it is True or False.

Example: Evaluating Strings and Numbers

python
1
2
print(bool("Hello"))  # True
print(bool(15))  # True

Example: Evaluating Variables

python
1
2
3
4
5
x = "Hello"
y = 15

print(bool(x))  # True
print(bool(y))  # True

Most Values are True

In Python, almost any value will evaluate to True if it contains data or content.

  • Non-empty strings are True.
  • Non-zero numbers are True.
  • Non-empty lists, tuples, sets, and dictionaries are True.

Example: Values that return True

python
1
2
3
print(bool("abc"))  # True
print(bool(123))  # True
print(bool(["apple", "cherry", "banana"]))  # True

Values that are False

Some specific values evaluate to False in Python:

  • False
  • None
  • 0 (zero)
  • Empty sequences like "", (), [], and empty collections like {} (empty dictionary).

Example: Values that return False

python
1
2
3
4
5
6
7
print(bool(False))  # False
print(bool(None))  # False
print(bool(0))  # False
print(bool(""))  # False
print(bool(()))  # False
print(bool([]))  # False
print(bool({}))  # False

Key Takeaways

  • Booleans are used to evaluate expressions and control the flow of logic in Python.
  • Values like non-empty strings, non-zero numbers, and non-empty collections evaluate to True, while False, None, 0, and empty collections evaluate to False.
  • You can use the bool() function to evaluate any value and determine its truthiness.

Frequently Asked Questions