Self-Study Notes: Boolean Variables in
Python
1. What is a Boolean Variable?
A Boolean variable is a variable that can only store two values:
- True
- False
In Python, Booleans are written with a capital T and capital F.
Example:
is_happy = True
is_sad = False
2. Boolean Values from Comparisons
Most Boolean values come from comparison operators. Python checks a condition and gives
either True or False.
Examples:
print(10 > 5) # True
print(3 == 7) # False
print(4 <= 4) # True
print(8 != 2) # True
Common comparison operators:
- == : equal to
- != : not equal to
- > : greater than
- < : less than
- >= : greater than or equal to
- <= : less than or equal to
3. Using Boolean Variables
You can store the result of a comparison in a variable:
x = 10
y = 20
is_greater = x > y
print(is_greater) # False
4. Boolean in Decision Making
Boolean values are very useful in if statements.
is_logged_in = True
if is_logged_in:
print("Welcome, user!")
else:
print("Please log in.")
Another example:
age = 18
if age >= 18:
print("You can vote.")
else:
print("You are too young to vote.")
5. Boolean Operators
Booleans can be combined using and, or, and not.
x=7
print(x > 5 and x < 10) # True
print(x > 10 or x == 7) # True
print(not(x == 7)) # False
Meaning:
- and → True if both are True
- or → True if at least one is True
- not → reverses True/False
6. Real-Life Examples in Python
Password check:
password = "1234"
if password == "1234":
print("Access granted!")
else:
print("Access denied!")
Weather example:
temperature = 25
is_raining = False
if temperature > 20 and not is_raining:
print("Great weather for a walk!")
else:
print("Better stay indoors.")
7. Summary
- Boolean variables store only True or False.
- They are often created by comparison operators.
- They are important for decision making (if statements) and logical operations (and, or,
not).
- Booleans allow programs to make choices and act differently depending on conditions.