• 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
1print(10 > 9)  # True
2print(10 == 9)  # False
3print(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
1print(bool("Hello"))  # True
2print(bool(15))  # True

Example: Evaluating Variables

python
1x = "Hello"
2y = 15
3
4print(bool(x))  # True
5print(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
1print(bool("abc"))  # True
2print(bool(123))  # True
3print(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
1print(bool(False))  # False
2print(bool(None))  # False
3print(bool(0))  # False
4print(bool(""))  # False
5print(bool(()))  # False
6print(bool([]))  # False
7print(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