EX | Boolean |
What are Booleans?
Booleans are a data type that can hold one of two values: True
or False
.
They are used to represent truth values and are essential for making decisions in your programs.
Booleans can be thought of as answers to yes/no questions — for example, “Is the user logged in?” or “Has the task been completed?”.
In Python, the Boolean values start with a capital letter: True
and False
, and they are considered basic building blocks of logic in programming.
You can assign Boolean values to variables just like any other data type:
x = True
y = False
A helpful way to imagine Booleans is to think of a light switch:
- When the switch is on, it’s like
True
— the condition is active. - When the switch is off, it’s like
False
— the condition is inactive.
This simple concept forms the basis for how programs make decisions and control what happens under certain circumstances.
Boolean Expressions
Boolean expressions are expressions that evaluate to either True or False. They are commonly used in conditional statements and loops. There are two kind of operators. Relational and Logical Operators.
a = 10
b = 5
# Greater than
print(a > b)
# Output: True
# Less than
print(a < b)
# Output: False
# Equal to
print(a == b)
# Output: False
# Not equal to
print(a != b)
# Output: True
# Greater than or equal to
print(a >= b)
# Output: True
# Less than or equal to
print(a <= b)
# Output: False