Python Booleans
1. What is a Boolean?
A Boolean means True or False.
In Python, Boolean has only two values:
-
True -
False
Booleans are used to make decisions in programs.
2. Creating Boolean Variables
You can store True or False in a variable.
Example
is_raining = True
is_sunny = False
print(is_raining)
print(is_sunny)
3. Boolean from Comparisons
When you compare two values, Python returns True or False.
Example
a = 10
b = 5
print(a > b) # True
print(a < b) # False
print(a == b) # False
4. Boolean in if Statement
Booleans are mostly used in if–else.
Example
age = 18
if age >= 18:
print("You are an adult")
else:
print("You are a child")
5. Boolean from User Input
Example
password = "python123"
user_input = input("Enter password: ")
print(user_input == password)
Output will be True or False.
6. Boolean with Logical Operators
Python has three logical operators:
-
and -
or -
not
AND
age = 20
has_id = True
print(age >= 18 and has_id)
OR
is_weekend = False
is_holiday = True
print(is_weekend or is_holiday)
NOT
is_logged_in = False
print(not is_logged_in)
7. Using bool() Function
The bool() function converts values into True or False.
Examples
print(bool(1)) # True
print(bool(0)) # False
print(bool("Hi")) # True
print(bool("")) # False
8. Common Beginner Mistakes
❌ Using Wrong Case
true
false
✔ Correct:
True
False
❌ Using = Instead of ==
if x = 5:
✔ Correct:
if x == 5:
9. Simple Practice Examples
Example 1: Check Even or Odd
num = 8
print(num % 2 == 0)
Example 2: Login Check
username = "admin"
password = "1234"
user = input("Username: ")
pwd = input("Password: ")
if user == username and pwd == password:
print("Login successful")
else:
print("Login failed")
Example 3: Age Check
age = int(input("Enter age: "))
print(age >= 18)
10. Summary (Python Booleans)
✔ Boolean means True or False
✔ Used in comparisons
✔ Used in if–else
✔ Logical operators control decisions
✔ bool() converts values
📘 Perfect for Beginner eBook
This chapter is ideal for:
-
Python beginner books
-
School & college students
-
Self-learners
If you want next, I can write:
-
Python Operators
-
if–else statements (easy)
-
Boolean Exercises
-
Loops (for, while)
-
MCQs with answers
Just tell me 😊
