2.2 Booleans (bool)
Understanding the Boolean data type (bool), its two possible values (True and False), and its fundamental importance in control structures and logical operations.
The Boolean data type (named after the mathematician George Boole) is one of the most fundamental types in programming. It can only have two possible values: `True` and `False`. These two values are keywords in Python and must always be capitalized.
Booleans are the basis for making decisions in code. They are constantly used in logical operations and, most importantly, as the result of comparison operators, determining the execution flow of a program through control structures like `if` statements.
Booleans as a Result of Comparisons
Whenever you compare two values using operators like `==` (equal), `!=` (not equal), `>` (greater than), `<` (less than), etc., the result produced is always a Boolean value.
# Boolean value examples
is_day = True
is_raining = False
print(f"Is it day? {is_day}, Type: {type(is_day).__name__}")
print(f"Is it raining? {is_raining}, Type: {type(is_raining).__name__}")
print("\n--- Booleans from Comparisons ---")
# Result of comparisons
is_greater = (10 > 5)
is_equal = (7 == 7)
is_not_equal = (4 != 4)
is_less_or_equal = (10 <= 10)
print(f"Is 10 greater than 5? {is_greater} (Type: {type(is_greater).__name__})")
print(f"Is 7 equal to 7? {is_equal}")
print(f"Is 4 not equal to 4? {is_not_equal}")
print(f"Is 10 less than or equal to 10? {is_less_or_equal}")
Explore More with AI
Use AI to generate new examples, delve deeper into theory, or get your questions answered.