0% found this document useful (0 votes)
2 views3 pages

Boolean Variables in Python

Boolean variables in Python can only store two values: True and False, and are often derived from comparison operators. They are crucial for decision making in if statements and can be combined using logical operators such as and, or, and not. This allows programs to make choices and behave differently based on conditions.

Uploaded by

milaziamidu8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Boolean Variables in Python

Boolean variables in Python can only store two values: True and False, and are often derived from comparison operators. They are crucial for decision making in if statements and can be combined using logical operators such as and, or, and not. This allows programs to make choices and behave differently based on conditions.

Uploaded by

milaziamidu8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

You might also like