Python Boolean Operators
Boolean operators are used to combine multiple conditions.
They always return either True or False.
1. and Operator
The and operator returns True only when both conditions are True.
Syntax
condition1 and condition2
Truth Table
condition1 condition2 result
True True True
True False False
False True False
False False False
Why it is used
Used when all conditions must be satisfied together.
Example: Check if a person is eligible to vote (age >= 18 and has voter ID).
age = 20
has_id = True
if age >= 18 and has_id:
print("Eligible to vote")
else:
print("Not eligible")
2. or Operator
The or operator returns True if at least one condition is True.
Syntax
condition1 or condition2
Truth Table
condition1 condition2 result
True True True
True False True
False True True
False False False
Why it is used
Used when any one condition is enough.
Example: Check if a student gets a holiday (weekend or public holiday).
is_weekend = False
is_holiday = True
if is_weekend or is_holiday:
print("Holiday")
else:
print("Working day")
Programs
1. Write a program to check if a number is between 10 and 50.
num = 25
if num >= 10 and num <= 50:
print("Number is in range")
else:
print("Number is out of range")
2. Write a program to check if a number is divisible by 2 or 5.
num = 10
if num % 2 == 0 or num % 5 == 0:
print("Divisible by 2 or 5")
else:
print("Not divisible")
3. Write a program to check if a number is positive and even.
num = 8
if num > 0 and num % 2 == 0:
print("Positive even number")
else:
print("Condition not satisfied")
4. Allow withdrawal only if balance is greater than 1000 and PIN is correct.
balance = 5000
pin_correct = True
if balance > 1000 and pin_correct:
print("Withdrawal allowed")
else:
print("Transaction denied")
5. Approve a loan if income is greater than 25000 or credit score is above 700.
income = 20000
credit_score = 750
if income > 25000 or credit_score > 700:
print("Loan Approved")
else:
print("Loan Rejected")
6. Allow ATM access if card is valid and account is active.
card_valid = True
account_active = True
if card_valid and account_active:
print("Access granted")
else:
print("Access denied")
7. Apply discount if total price is greater than 1000 and user is a member.
price = 1500
is_member = True
if price > 1000 and is_member:
print("Discount applied")
else:
print("No discount")
8. Provide free delivery if price is greater than 500 or a coupon is applied.
price = 300
coupon = True
if price > 500 or coupon:
print("Free delivery")
else:
print("Delivery charges apply")
9. Confirm order only if item is in stock and payment is successful.
in_stock = True
payment_done = True
if in_stock and payment_done:
print("Order placed successfully")
else:
print("Order failed")
Challenge Questions
1. Write a program to check if a student passes based on the following conditions:
• Marks in all subjects should be greater than or equal to 40
• Overall average should be greater than or equal to 50
If both conditions are satisfied, print "Pass", otherwise print "Fail".
2. Write a program to allow ATM withdrawal only if:
• Balance is greater than or equal to withdrawal amount
• AND either PIN is correct OR OTP is verified
Print "Transaction Successful" or "Transaction Failed".
3. Write a program to apply an offer based on:
• User gets discount if (price > 1000 AND user is premium member)
• OR if festival offer is active
Print "Offer Applied" or "No Offer".